Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
# on all examples here, specifying authentication user and password is not needed on specific cases like follows
# * server/client are both in AD Domain and also logon user on Client is allowed to access to Share
# * even on workgroup environment, a user exists on Client that name is the same with the user that is allowed to access to Share on Server and also their password is the same
### with New-SmbMapping
# -LocalPath [drive letter]
# -RemotePath [\\(Server name)\(Share name)]
# -UserName [authenticated username] -Password [password]
# -Persistent [$true|$false]
# $true means enabled, $false means mapping is valid only on current session
PS C:\Users\serverworld> New-SmbMapping -LocalPath "Z:" `
-RemotePath "\\rx-7.srv.world\Share01" `
-UserName "Serverworld" -Password "P@ssw0rd01" `
-Persistent $true
# confirm
PS C:\Users\serverworld> Get-SmbMapping
Status Local Path Remote Path
------ ---------- -----------
OK Z: \\rx-7.srv.world\Share01
PS C:\Users\serverworld> ls Z:\
Directory: Z:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2019/09/18 19:46 new folder
-a---- 2019/09/18 21:03 0 new text.txt
-a---- 2019/09/18 20:02 16 testfile.txt
# to disconnect Share, run like follows
PS C:\Users\serverworld> Remove-SmbMapping -LocalPath "Z:"
### with New-PSDrive
# -Name [any drive name you like]
# if you set drive persistently with [-Persist], it needs to specify drive letter like "Z:", "Y:" and so on)
# -PSProvider [FileSystem]
# if filesystem, specify [FileSystem], but if registry, specify [Registry]
# -Root [\\(Server name)\(Share name)]
# -Credential (specify authenticated username/password)]
# example below uses a user (Serverworld/P@ssw0rd01)
# -Persist (if with this option, mapping keeps persistently, if not specify, mapping is valid only on current session)
PS C:\Users\serverworld> New-PSDrive -Name "Share01" `
-PSProvider FileSystem `
-Root "\\rx-7.srv.world\Share01" `
-Credential (New-Object PSCredential("Serverworld", (ConvertTo-SecureString -AsPlainText "P@ssw0rd01" -Force)))
# confirm
PS C:\Users\serverworld> Get-PSDrive -Name "Share01" | Format-Table -AutoSize
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
Share01 FileSystem \\rx-7.srv.world\Share01
PS C:\Users\serverworld> ls Share01:\
Directory: \\rx-7.srv.world\Share01
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2019/09/18 10:46 new folder
-a---- 2019/09/18 13:03 0 new text.txt
-a---- 2019/09/18 11:02 16 testfile.txt
# to disconnect Share, run like follows
PS C:\Users\serverworld> Remove-PSDrive "Share01"
### with net use
# net use [drive letter (optional)] [\\(Server name)\(Share name)] [/user:(username)] [password (if not specified, need to input intaractively)] [/persistent:(yes|no) (default is [Yes])]
PS C:\Users\serverworld> net use "\\rx-7.srv.world\Share01" /user:Serverworld P@ssw0rd01 /persistent:no
The command completed successfully.
PS C:\Users\serverworld> ls "\\rx-7.srv.world\Share01"
Directory: \\rx-7.srv.world\Share01
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2019/09/18 10:46 new folder
-a---- 2019/09/18 13:03 0 new text.txt
-a---- 2019/09/18 11:02 16 testfile.txt
# to disconnect Share, run like follows
PS C:\Users\serverworld> net use "\\rx-7.srv.world\Share01" /delete
|