Search code examples
pathmappingshareletter

network share script - auto check drive letter and path


I have this script that adds a path based on the username:

$credential = Get-Credential
$user=$credential.GetNetworkCredential().UserName
New-PSDrive -Name x -PSProvider FileSystem -Root "\\192.168.1.1\$($user)" -Credential $credential -persist

I'm trying to make it more robust, how can I edit it to check which drive letter is available and if the network path is already mapped with an existing drive letter, so no error will be made if the end user runs it multiple times.


Solution

  • Free drive letters:

    ls function:[d-z]: -n | ?{ !(test-path $_) }
    

    Credit goes to network share script - auto check drive letter and path