Share management in PowerShell

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:

Comments:

Unknown Identity
Unknown Identity 28.12.2010 15:37 (GMT+3)

THX for sharing this great utils. lp,J

Unknown Identity
Unknown Identity 21.02.2011 12:47 (GMT+3)

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+3)

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+3)

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

Frank Peter Schultze
Frank Peter Schultze 19.01.2012 19:16 (GMT+3)

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+3)

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+3)

ditto

Vadims Podans
Vadims Podans 24.04.2012 16:55 (GMT+3)

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

c
c 25.04.2012 03:06 (GMT+3)

ditto

Vadims Podans
Vadims Podans 28.04.2012 05:04 (GMT+3)

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+3)

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+3)

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+3)

I'm not aware about other ways.

Randy
Randy 11.12.2012 23:52 (GMT+3)

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+3)

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+3)

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+3)

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

Josh
Josh 08.02.2013 08:23 (GMT+3)

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+3)

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+3)

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+3)

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+3)

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+3)

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

AndyF
AndyF 17.01.2014 19:57 (GMT+3)

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+3)

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+3)

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+3)

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+3)

-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+3)

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+3)

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+3)

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+3)

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+3)

what "Get-Command -Module shareutils" returns?

chris
chris 14.04.2015 10:25 (GMT+3)

hey

Captcha