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

THX for sharing this great utils. lp,J

Unknown Identity

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

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

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

Frank Peter Schultze

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

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

ditto

Vadims Podans

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

c

ditto

Vadims Podans

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

WEH

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

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

I'm not aware about other ways.

Randy

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

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

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

Vadims Podans

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

Josh

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

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

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

Juan mamani

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

Juan mamani

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

Bart

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

AndyF

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

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

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

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

-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

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

Vadims Podans

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

Julian Freeman

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

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

what "Get-Command -Module shareutils" returns?

Travis L

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

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

Thanks for sharing, really great !

Bitnugger

"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

Supplement:

With two letters it works however:

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

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

Vyacheslav

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

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

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

Vadims Podāns

download link is fixed.

Scott Antkowiak

I'm not able to get this module to import.  I believe it may be because the cert expired in 2015?  I'm getting the following from an elevated PowerShell session:

Import-Module : The following error occurred while loading the extended type data file: ,
C:\Users\santkowiak\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml: The file was skipped
because of the following validation exception: File
C:\Users\santkowiak\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml cannot be loaded. A
certificate chain processed, but terminated in a root certificate which is not trusted by the trust provider..
At line:1 char:1
+ Import-Module ShareUtils
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Import-Module], RuntimeException
    + FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand

Vadims Podāns

The package is signed by an untrusted certificate. You need to unblock ZIP archive and configure PowerShell execution policy to RemoteSigned.


Post your comment:

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