Powershell learning path-Folder sharing and disk ing

Source: Internet
Author: User

Powershell learning path-Folder sharing and disk ing
GuideIn the Linux environment, we can easily operate everything through commands. In Windows, Powershell is also a rising star, providing a large number of cmdlet and horizontal expansion of c. The following will be a small Editor to lead you to share folders through Powershell. Of course, the old cmd veteran will not be left empty in this article.

Folder sharing Overview

Shared Folders are widely used. The client manages files on the server and directly transfers files from the lan. in linux, you can simply install the smaba protocol and use it after simple configuration. In windows, you can enable this function through graphical operations. Of course, with a little geek style, we manage it through powershell (a small number of cmd appears.

ProcedureView share list

In powershell, we can obtain sharing information by executing the following cmdlet:

Lambda Get-WmiObject-Class Win32_ShareName Path Description ---- ----------- ADMIN $ C: \ Windows Remote Management C $ C: \ default shared D $ D: \ default shared E $ E: \ shared IPC $ remote IPCUsers C: \ Users by default

Similarly, in cmd

Lambda net share Name resource annotation ------------------------------------------------------------------------------- C $ C: \ default share D $ D: \ default share E $ E: \ default share IPC $ remote IPCADMIN $ C: \ Windows Remote Users C: \ Users command is successfully completed.
Create a shared folder

Crazy Powershell

# SHARE Name $ ShareName = 'testshare' # SHARE Path $ Path = 'd: \ SHARE 'If (! (Get-WmiObject-Class Win32_Share-Filter "name = '$ ShareName'") {$ Shares = [WMICLASS] "WIN32_Share" $ Shares. create ($ Path, $ ShareName, 0 ). returnValue} else {Write-Warning "$ ShareName has been sharing !! "}

If you have the Administrator permission for the remote machine, you can also use WMI to create a shared folder on the remote machine. The following Code creates a shared folder on the remote host:

# SHARE Name $ ShareName = 'testshare' # SHARE Path $ Path = 'd: \ SHARE '# remote host name $ Server = 'server' If (! (Get-WmiObject-Class Win32_Share-Filter "name = '$ ShareName'") {$ Shares = [WMICLASS] "\ $ Server \ root \ cimv2: WIN32_Share "$ Shares. create ($ Path, $ ShareName, 0 ). returnValue} else {Write-Warning "$ ShareName has been sharing !! "}

Low-profile cmd

: We recommend that you first view the current shared folder and then create the file. net share TestShare = D: \ SHARE/users: 25/remark: "test share of the folder"

We can easily enable the sharing status of a folder and access it through the UNC path. After file sharing is created, let's see how to use it.

Drive ing and shared access

Next, we will discard the operations on the graphical interface (if you do not like this, you can view it through the "network" of the network neighbor, or right-click the ing network drive under the computer icon ), let's use the command to enable it.

Powerful Powershell

Create a network drive ing temporarily:

(New-Object -ComObject WScript.Network).MapNetworkDrive("Z:", "\\TEST-PC\USERS")

Create a persistent network drive ing:

# Add the-Persist parameter to New-PSDrive to make the drive visible outside PowerShell. # To create a permanent network drive, add-Scope Global. /# If New-PSDrive runs outside the global scope (for example, in a script), the drive only appears in the file manager when the script is running. New-PSDrive-Name Z-PSProvider FileSystem-Root \ TEST-PC \ USERS-Persist-Scope Global

Small cmd

: Although the following command can use this disk ing in cmd, it cannot be loaded using the resource manager. Net use Z: \ TEST-PC \ USERS: Here is to map the systemroot folder to z: Drive, can be loaded using "assumer.exe", but unfortunately cannot use the network path. Subst Z: $ env: systemroot

After completing the above work, the resource manager will display the network path icon you want to access.

Delete share

If you no longer need to use this shared folder, you can detach the network drive and delete the shared folder from the shared host.

Powershell

$Shares = Get-WMIObject Win32_Share | Where {$_.Name -eq ""}Foreach ($Share in $Shares) {   $Share.Delete()}

Cmd

net share TestShare /delete
Summary
  1. To create a shared folder, you must enable network sharing and discovery in advance.
  2. Folder permission control and shared permission control must be completed in advance.
  3. You can skip authentication by configuring the primary key.
cmdkey /add:targetname /user:username /pass:password

Links to Powershell Management Sharing: https://msdn.microsoft.com /...

Address: http://www.linuxprobe.com/powershell-win32-share.html


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.