Below is the powershell script sample on how to call NET USE command to create a shared folder on any remote server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | $netShareSettings += @{ "Server" = "TargetServer" ; "Path" = "E:\MyFolderToBeShared" ; "ShareName" = "SharedFolder" ; "UserAndPermission" = ( "Domain\user1;FULL" , "Domain\User2;FULL" ); } foreach ( $netShareSetting in $netShareSettings ) { $script = { $userParam = " "; foreach($pm in $args[0].UserAndPermission) { $arr = $pm.Split(" ; "); $userName = $arr[0]; $perm = $arr[1]; $userParam+= " /grant:` "{0}`,{1}`" " -f $userName , $perm } $command = "net share {0}=`"{1}`" {2}" -f $args [0].ShareName, $args [0].Path, $userParam $msg = " Executing Command: '{0}' on '{1}'" -f $command , $args [0].Server $msg = Invoke-Expression $command | Out-String $msg = " Results: " + $msg .Trim(); Write-Host $msg } Invoke-Command -ScriptBlock $script -ComputerName $netShareSetting .Server -ArgumentList $netShareSetting } |