Creating and Manipulating Network Shares with Powershell

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.

  • Create – Class method that initiates sharing for a server resource.
  • Delete – Class method that deletes a share name from a server’s list of shared resources, disconnecting connections to the shared resource.
  • GetAccessMask – Returns the access rights to the share, held by the user or group on whose behalf the instance is returned. You should use this method in place of the AccessMask property which is always NULL.
  • SetShareInfo – Class method that sets the parameters of a shared resource.

How do I create a network share from Powershell?

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"))

Create Method of the Win32_Share Class

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);

Parameters

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" 

How do I remove a network share from Powershell?

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)

Delete Method of the Win32_Share Class

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();

Parameters

This method has no parameters.

Return Values

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

Alternative method to delete a network share

Net share works as well:

PS> net share tempshare /delete

tempshare was deleted successfully.

How do I map network drive from Powershell?

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

Related links

Jul 25, 2017

Similar articles