Guide |
In the Linux environment, we are easily able to manipulate everything by command, and in Windows, PowerShell is a rising star, providing a large number of cmdlets and C # horizontal expansion. The following will be a small series to lead you through the PowerShell folder sharing, of course, the article will not snub the CMD this veteran. |
folder sharing Overview
The application of the shared folder is very extensive, the client carries on the file management to the server side, the LAN file through balls and so on, under the Linux, can install the Smaba protocol simply, after the simple configuration can use. Under Windows, this feature can be turned on graphically, and of course with a bit of a geek style, we manage it through PowerShell (a small amount of cmd).
Operation Steps
View shared list
Within PowerShell, we can obtain shared information by executing the following cmdlet:
Λget-wmiobject-class win32_sharename Path Description---- ---- -----------admin$ C:\Windows Manage C $ remotely \ c \ default share d$ d:\ default share e$ e:\ default share ipc$ remote ipcusers C:\Users
In the same vein, under CMD, you can also
Λnet Share share name resource Note-------------------------------------------------------------------------------C $ \ c \ default share d$ d:\ default share e$ e:\ default share ipc$ remote ipcadmin$ C:\Windows Remote administration users The C:\Users command completed successfully.
Create a shared folder
The Crazy PowerShell
# share name $sharename = ' testshare ' # shared 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 administrator privileges on the remote machine, you can also use WMI to create a new shared folder on the remote machine, and the following is the code to create the shared folder on the remote host:
# share name $sharename = ' testshare ' # shared 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-key cmd
:: It is recommended to view the current shared folder before creating the operation net share testshare=d:\share/users:25/remark: "Test share of the A folder"
We can easily open the shared state of a folder and access it through a UNC path. After creating the file share, let's take a look at how to use it.
drive mapping and shared access
Next, we discard the graphical interface operation (if you do not like to do so, can be viewed through the Network Neighborhood "" ", or right-click on the computer icon to select the mapped network drive), we have to use the command to enable it.
The intrepid PowerShell
Temporarily create a network drive map:
(New-object-comobject wscript.network). MapNetworkDrive ("Z:", "\\TEST-PC\USERS")
To create a persistent network drive map:
# new-psdrive plus the-persist parameter makes the drive visible outside of PowerShell. # to really create a permanent network drive, make sure to add-scope Global. /# if New-psdrive runs outside of the global scope (for example, running in a script), the drive will only appear in the File Manager when the script is run. New-psdrive-name Z-psprovider filesystem-root \\TEST-PC\USERS-Persist-Scope Global
Small cmd
:: The following command, although this disk mapping can be used in cmd, cannot be loaded with the resource manager. NET use Z: \\TEST-PC\USERS: Here is the mapping of the systemroot folder to Z: drive, which can be loaded with "Explorer.exe", but the network path cannot be used. Subst Z: $env: SystemRoot
After completing the work as above, no surprises, your explorer will appear with the icon of the network path you want to access.
Delete Share
If you do not need to use this shared folder again, you can uninstall the network drive and delete the share on the shared host.
Powershell
$Shares = Get-wmiobject Win32_Share | Where {$_. Name-eq ""}foreach ($Share in $Shares) { $Share. Delete ()}
Cmd
NET share Testshare/delete
Summary
- Creating a shared folder requires that you enable network sharing and discovery beforehand.
- You need to do a good job of folder permissions control and shared permissions control in advance.
- By configuring Cmdkey, you can exempt from identity authentication
Cmdkey/add:targetname/user:username/pass:password
PowerShell Learning Path-folder sharing and disk mapping