Surprise, it's me again! Today I have finished network share management module for PowerShell. I want to thank Shay Levy who helped me with module-related stuff.

With this module you will be able to:

  • List share (or shares) from local or from remote computers;
  • Create new share with various settings;
  • Remove share (actually this not remove folder itself, but stops sharing);
  • Add/Set/Remove share permissions (note that share permissions and NTFS rights are not the same).

Here are module installation instructions:

The ZIP file contains the module files only and you need to extract its content to one of two places:

  1. %USERPROFILE%\Documents\WindowsPowerShell\Modules
  2. %WINDIR%\System32\WindowsPowerShell\v1.0\Modules (need admin privileges)
  3. If the directory tree (of one of the above) doesn't exist then you should manually create it.

Check if the module is installed correctly, from your PowerShell session type:

PS C:\> Get-Module -ListAvailable

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Manifest   ShareUtils                {}


PS C:\>

If you don't see the above result then the module was not installed correctly. Make sure the module directory exists under "%USERPROFILE%\Documents\WindowsPowerShell\Modules"

Importing the module:

PS C:\> Import-Module ShareUtils

Note: An elevated PowerShell session is needed to load the module. If the session is not elevated a warning message is written to the console and you won't be able to use any of the module’s functions.

PS C:\> Get-Command -Module shareutils

CommandType     Name                                                Definition
-----------     ----                                                ----------
Filter          Add-SharePermission                                 ...
Function        Get-Share                                           ...
Function        New-Share                                           ...
Filter          Remove-Share                                        ...
Filter          Remove-SharePermission                              ...
Filter          Set-Share                                           ...
Filter          Set-SharePermission                                 ...


PS C:\>

Each function (filter) has its own help. To get a help you may run the following command:

Get-Help <FunctionName>

And download link:


Share this article:

Comments:

Unknown Identity
Unknown Identity 28.12.2010 15:37 (GMT+2) Share management in PowerShell

THX for sharing this great utils. lp,J

Unknown Identity
Unknown Identity 21.02.2011 12:47 (GMT+2) Share management in PowerShell

How fun! I linked to this on last weekend's weekly roundup and am just now getting around to letting you know. (I guess we were busy celebrating Easter!) Thanks so much for sharing!

Unknown Identity
Unknown Identity 26.04.2011 03:23 (GMT+2) Share management in PowerShell

I imported the modules as instructed. I can see "ShareUtils" under -ListAvailable. Get-Command -Module ShareUtils does not return and results. No help listed under Get-Help. Please-Help!

Unknown Identity
Unknown Identity 27.04.2011 17:01 (GMT+2) Share management in PowerShell

It looks like you haven't imported it via Import-Module cmdlet.

Frank Peter Schultze
Frank Peter Schultze 19.01.2012 19:16 (GMT+2) Share management in PowerShell

Thanks for sharing shareutils. There's an issue with AllowMaximum (default $true), and MaximumAllowed (default 0). If I leave the default settings in order to allow maximum connection with the share, the script will create the share with 0 allowed connections. (Tested on Windows 7 and Windows Server 2008 R2) As a workaround I replaced each $ShareInfo.MaximumAllowed with $(if (!($ShareInfo.AllowMaximum)) {$ShareInfo.MaximumAllowed}) But, this approach is not perfect because it's not possible to toggle between allow maximum and allow a fixed number.

PS Newbie
PS Newbie 22.04.2012 06:05 (GMT+2) Share management in PowerShell

After extracting the zip to its own folder in %WINDIR%\System32\WindowsPowerShell\v1.0\Modules, when I launched 'Windows Powershell Modules' I got the following error: WARNING: File C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ShareUtils\ShareUtils.psm1 cannot be loaded. The contents of file C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ShareUtils\ShareUtils.psm1 may have been tampered because the hash of the file does not match the hash stored in the digital signature. The script will not execute on the system. Please see "get-help about_signing" for more details..

c
c 24.04.2012 08:30 (GMT+2) Share management in PowerShell

ditto

Vadims Podans
Vadims Podans 24.04.2012 16:55 (GMT+2) Share management in PowerShell

This can be due to invalid execution policy. Start elevated PowerShell and type: Set-ExecutionPolicy RemoteSigned

c
c 25.04.2012 03:06 (GMT+2) Share management in PowerShell

ditto

Vadims Podans
Vadims Podans 28.04.2012 05:04 (GMT+2) Share management in PowerShell

Additionally you need to unblock downloaded files (right-click on each and click Unblock in file properties).

WEH
WEH 20.10.2012 10:54 (GMT+2) Share management in PowerShell

Does not seem to work on 2008 R2 Cluster Shares. I created a share called TEST$ and it could not find it when I did a get-share. Bummer.

Randy
Randy 11.12.2012 09:23 (GMT+2) Share management in PowerShell

Is there any way to accomplish this without WMI? I'm looking for a way to remotely manage shares on a non-Windows NAS storage system. I can use Computer Management MMC to connect to the NAS remotely and manage the shares, but looking for a way to do it without WMI since it's not supported on the NAS.

Vadims Podans
Vadims Podans 11.12.2012 16:16 (GMT+2) Share management in PowerShell

I'm not aware about other ways.

Randy
Randy 11.12.2012 23:52 (GMT+2) Share management in PowerShell

Is there any way to accomplish this without WMI? I'm looking for a way to remotely manage shares on a non-Windows NAS storage system. I can use Computer Management MMC to connect to the NAS remotely and manage the shares, but looking for a way to do it without WMI since it's not supported on the NAS.

Randy
Randy 12.12.2012 05:52 (GMT+2) Share management in PowerShell

Is there any way to accomplish this without WMI? I'm looking for a way to remotely manage shares on a non-Windows NAS storage system. I can use Computer Management MMC to connect to the NAS remotely and manage the shares, but looking for a way to do it without WMI since it's not supported on the NAS.

OzDM
OzDM 30.01.2013 14:19 (GMT+2) Share management in PowerShell

Can you please fix the code or show the syntax required to allow maximum connections to a share.

Vadims Podans
Vadims Podans 31.01.2013 05:36 (GMT+2) Share management in PowerShell

Are you sure that new created share do not allow maximum connections?

Josh
Josh 08.02.2013 08:23 (GMT+2) Share management in PowerShell

Yes, I am sure. the default if no entry is put in is 0 so it creates a share with no user access allowed.

Mylex
Mylex 05.03.2013 20:52 (GMT+2) Share management in PowerShell

In ShareUtils.psm1 change line 46 from "public int MaximumAllowed;" to "public long MaximumAllowed;",and in New-Share.ps1 change line 50 from from "[int]$MaximumAllowed = 0" to "[long]$MaximumAllowed = 4294967295". This fixed it for me.

Niclas Eriksson
Niclas Eriksson 22.10.2013 01:26 (GMT+2) Share management in PowerShell

The fix for MaximumAllowed above worked great for me as well on a Server 2012. Thanks!

Juan mamani
Juan mamani 23.10.2013 04:21 (GMT+2) Share management in PowerShell

Im having problems running command Add-SharePermission, could please provide me an example to run this command?

Juan mamani
Juan mamani 23.10.2013 04:43 (GMT+2) Share management in PowerShell

Im having problems running command Add-SharePermission, could please provide me an example to run this command?

Bart
Bart 14.11.2013 02:06 (GMT+2) Share management in PowerShell

Thanks for sharing this module. Helped me out creating shares on remote servers.

AndyF
AndyF 17.01.2014 19:57 (GMT+2) Share management in PowerShell

Thanks for creating this module, very useful. I have a problem however, I am unable to create a share with the format "\\servername\e$\directory\directory", " sharename$" No error messages, just doesn't work. If I use a local path, it works fine. I also get the same result if I use the [WMICLASS]�WIN32_Share� method with $ Shares.Create. Any ideas? thanks for the help

AndyF
AndyF 17.01.2014 20:29 (GMT+2) Share management in PowerShell

Thanks for creating this module, very useful. I have a problem however, I am unable to create a share with the format "\\servername\e$\directory\directory", " sharename$" No error messages, just doesn't work. If I use a local path, it works fine. I also get the same result if I use the [WMICLASS]�WIN32_Share� method with $ Shares.Create. Any ideas? thanks for the help

Michael Becker
Michael Becker 18.01.2014 05:10 (GMT+2) Share management in PowerShell

Thank you for this module, it has seriously saved me many of hours of work trying to make this happen in my new user creation script!

Michael Becker
Michael Becker 18.01.2014 05:13 (GMT+2) Share management in PowerShell

Here's how I create my remote shares, you can try this: $Alias = $server = "" $HomeFolderMasterDir="\\$server\d$\\" new-item -path $HomeFolderMasterDir -name $Alias -type directory

Vadims Podans
Vadims Podans 20.01.2014 21:15 (GMT+2) Share management in PowerShell

-Path parameter expects the *local* path on remote server, not UNC (which is not supported). In your case, the -Path parameter shoud be "d:\".

mikel v
mikel v 22.01.2014 02:34 (GMT+2) Share management in PowerShell

The get-share cmdlet doesn't show any share using "virtual computer name" get-share -name "anysharename" -computername "clusterfileservername"

Vadims Podans
Vadims Podans 22.01.2014 19:15 (GMT+2) Share management in PowerShell

The module (and underlying API) do not support virtual names. Instead, you must specify cluster node names. It is by design.

Julian Freeman
Julian Freeman 01.07.2014 19:12 (GMT+2) Share management in PowerShell

Parsing a manually created share with a group having FULL rights is listed in the SecurityDescriptor property as Read. Other than that so far appears to be good.

Pete H
Pete H 10.07.2014 03:30 (GMT+2) Share management in PowerShell

I see the ModuleType as Script and the only exported command available is Test-ElevatedShell. Im running Win7 client talking to a Win2008r2 server. Is it my environment?

Vadims Podans
Vadims Podans 14.07.2014 21:09 (GMT+2) Share management in PowerShell

what "Get-Command -Module shareutils" returns?

Travis L
Travis L 16.09.2015 19:26 (GMT+2) Share management in PowerShell

I had to modify 'Get-Share' in order to get the correct value set on the Permission parameter by adding:

$Masks = @{2032127 = "FullControl"; 1245631 = "Change"; 1179817 = "Read"}

And then inserting the Permission line in where the Descriptor is being set:

$SD.Descriptor.DACL | %{
                    Write-Verbose "Create SecurityDescriptor object for current ACE"
                    $Descriptor = New-Object ShareUtils.SecurityDescriptor.SD
                    $Descriptor.User = $_.trustee.Name
                    $Descriptor.SIDString = $_.trustee.SIDString
                    $Descriptor.Domain = $_.trustee.Domain
                    $Descriptor.AccessMask = $_.AccessMask
                    $Descriptor.AceFlags = $_.AceFlags
                    $Descriptor.AceType = $_.AceType
                    $Descriptor.Permission = $Masks[$Descriptor.AccessMask]
                    $ShareInfo.SecurityDescriptor += $Descriptor
                }

Travis L
Travis L 16.09.2015 20:24 (GMT+2) Share management in PowerShell

Another fix when adding a permission to a share which has no currently existing shares:

Modify Add-SharePermission:

Write-Verbose "Add new ACE to the existing ACL"
    if ($OldSD) {
        Write-Verbose "Merging new ACE with existing ACLs"
        $ShareInfo.SecurityDescriptor = @(New-SD $user $AccessType $Permission) + $OldSD
    }
    else {
        Write-Verbose "Adding new ACE with existing ACLs (empty)"
        $ShareInfo.SecurityDescriptor = @(New-SD $user $AccessType $Permission)
    }

mprudhomme
mprudhomme 27.04.2017 19:47 (GMT+2) Share management in PowerShell

Thanks for sharing, really great !

Bitnugger
Bitnugger 29.04.2017 03:36 (GMT+2) Share management in PowerShell

"Get-Share U *" (as shown in the examples) does not work with me unfortunately.

PS C: \ WINDOWS \ system32> Get-Share

Name ComputerName Description
---- ------------ -----------
Images ILLUMINATI
FTP Share ILLUMINATI
Kodi ILLUMINATI
Pictures ILLUMINATI
Print $ ILLUMINATI printer driver
Users ILLUMINATI

PS C: \ WINDOWS \ system32> Get-Share U *
PS C: \ WINDOWS \ system32>

__

Thanks for sharing!

Bitnugger
Bitnugger 29.04.2017 03:41 (GMT+2) Share management in PowerShell

Supplement:

With two letters it works however:

PS C: \ WINDOWS \ system32> Get-Share Us *

Name ComputerName Description
---- ------------ -----------
Users ILLUMINATI

Vyacheslav
Vyacheslav 16.11.2017 16:56 (GMT+2) Share management in PowerShell

Bitnugger you find bug

in file Get-Share.ps1: 

replace  if ($share1 -ne $null -and $share1.Type -eq 0)  TO  if ($share1 -ne $null)

# if ($share1 -ne $null -and $share1.Type -eq 0) {
if ($share1 -ne $null) {
 

Sam
Sam 28.11.2017 03:12 (GMT+2) Share management in PowerShell

There is another bug where apostrophes in the share name are not escaped on the WMI queries within the script. This caused me problems in getting shares. the fix is to modify every "gwmi" line in every script that has the share name in the filter. Append .Replace("'","\'") to the share name and it will escape apostrophes correctly.

For example: Get-Share.ps1 line 49:

$share1 = gwmi Win32_Share -ComputerName $Computer -Filter "name LIKE '$($sharename.Replace("*","%").Replace("'","\'"))'"

 

Jay Patel
Jay Patel 06.11.2018 11:24 (GMT+2) Share management in PowerShell

Download link is not working..Can you please enable it.

Vadims Podāns
Vadims Podāns 06.11.2018 11:48 (GMT+2) Share management in PowerShell

download link is fixed.


Post your comment:

Please, solve this little equation and enter result below. Captcha