This article represents manipulating network shares with PowerShell on local or remote computer. You can create or delete network shares using Win32_Share.
The Win32_Share class represents a shared resource on a Windows system. This may be a disk drive, printer, inter-process communication, or other shareable device.
The Win32_Share class defines the following methods.
You can create a network share using the Win32_Share Create method:
(Get-WmiObject -List -ComputerName . | Where-Object -FilterScript {$_.Name -eq "Win32_Share"}).InvokeMethod("Create",("C:temp","TempShare",0,25,"temp share"))
The Create WMI class method initiates sharing for a server resource.
uint32 Create( string Path, string Name, uint32 Type, uint32 MaximumAllowed, string Description, string Password, Win32_SecurityDescriptor Access);
Path – Local path of the Windows share. For example, “C:\temp”.
Name – Passes the alias to a path set up as a share on a Windows system. Example, “TempShare”.
Type – Passes the type of resource being shared. Types includes disk drives, print queues, interprocess communications (IPC), and general devices. Can be one of the following values.
0 – Disk Drive 1 – Print Queue 2 – Device 3 – IPC 2147483648 – Disk Drive Admin 2147483649 – Print Queue Admin 2147483650 – Device Admin 2147483651 – IPC Admin
MaximumAllowed – Limit on the maximum number of users allowed to concurrently use this resource. Example: 25. This parameter is optional.
Description – Optional comment to describe the resource being shared. This parameter is optional. Example: “temp share”
Password – Password (when the server is running with share-level security) for the shared resource. If the server is running with user-level security, this parameter is ignored. This parameter is optional.
Access – Security descriptor for user level permissions. A security descriptor contains information about the permissions, owner, and access capabilities of the resource. Alternative method creation a network share
You can also create the share using net share in Windows PowerShell
net share tempshare=c:temp /users:25 /remark:"temp share"
You can remove a network share with Win32_Share. The following command will delete the share “TempShare”:
(Get-WmiObject -Class Win32_Share -ComputerName . -Filter "Name='TempShare'").InvokeMethod("Delete",$null)''
If TempShare doesn’t exist Powershell will show a message
You cannot call a method on a null-valued expression. At line:1 char:91 + (Get-WmiObject -Class Win32_Share -ComputerName . -Filter "Name='TempShare'") .InvokeMethod( <<<< "Delete",$null)
The Delete WMI class method deletes a share name from a server’s list of shared resources, disconnecting connections to the shared resource.
uint32 Delete();
This method has no parameters.
Returns one of the values in the following table or any other value to indicate an error.
0 – Success 2 – Access denied 8 – Unknown failure 9 – Invalid name 10 – Invalid level 21 – Invalid parameter 22 – Duplicate share 23 – Redirected path 24 – Unknown device or directory 25 – Net name not found
Net share works as well:
PS> net share tempshare /delete
tempshare was deleted successfully.
To map network drive, you can use the WScript.Network COM object.
The following command maps the share \\Server1\ShareFolder to local drive X:
(New-Object -ComObject WScript.Network).MapNetworkDrive("X:", \Server1sharefolder)
The net use command works as well:
net use X: \Server1sharefolder
Jul 25, 2017