Search code examples
windowspowershellcmdcommand-promptwmic

Add user access to a shared folder in remote computer


My objective is to add users to a shared folder in the remote server. I know that netshare command can be used to add users to local folder.

net share PathName=D:/Projects /GRANT:XXXX,FULL

When i run the above command in the local machine, it works fine.

Since the shared folder is present in remote server, i tried wmic and the psExec options. But both did not work. Not sure what i am missing here

wmic /node:ComputerName process call create "cmd.exe net share PathName=D:/Projects /GRANT:XXXX,FULL"

and

psExec \\ComputerName cmd.exe "net share PathName=D:/Projects /GRANT:XXXX,FULL"

Solution

  • Assuming you're running Windows 8 (Server 2012) or newer, use the Grant-SmbShareAccess cmdlet and a remote CIM session:

    $RemoteSession = New-CimSession -ComputerName RemoteComputerName
    Grant-SmbShareAccess -Name ShareName -AccountName XXXX -AccessRights Full -CimSession $RemoteSession
    

    On Windows 7, you can use Invoke-Command to run the net share command on the remote machine:

    $RemoteSession = New-PSSession -ComputerName RemoteComputerName
    Invoke-Command -Session $RemoteSession -ScriptBlock { net share PathName=D:/Projects /GRANT:XXXX,FULL }