Skip to content

Instantly share code, notes, and snippets.

@anderssonjohan
Created March 31, 2015 16:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anderssonjohan/8d3f958f29b4ae5c7802 to your computer and use it in GitHub Desktop.
Save anderssonjohan/8d3f958f29b4ae5c7802 to your computer and use it in GitHub Desktop.
Profile.ps1 that fixes the problem with missing PSDrives when running Powershell with elevated privileges
# Reconnect PSDrives for network connections when running with elevated privileges
# Fixes http://stackoverflow.com/questions/4742992/cannot-access-network-drive-in-powershell-running-as-administrator
$elevated = (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
if( $elevated ) {
net use | ?{ $_ -match ":\s+\\\\" -and !$_.StartsWith("Unavailable") } | %{
$tokens = $_.split(":")
$psdrivename = $tokens[0][$tokens[0].length-1]
$path = $tokens[1].trim().split(" ")[0].trim()
if( !(get-psdrive | ?{ $_.Name -eq $psdrivename } )) {
write-host ( "Restoring PSDrive for {0}: {1}" -f $psdrivename, $path )
new-psdrive $psdrivename FileSystem $path | out-null
}
}
}
@SeeCwriter
Copy link

This script only worked when I added a semi-colon at the end of each line within a bracket pair. Is this because of powershell changes since this script was written? I'm using PS 5.1.

@anderssonjohan
Copy link
Author

@SeeCwriter Huh? seems strange. Can you post your version here?

@SeeCwriter
Copy link

I tried your script as-is, and it didn't do anything. After playing with it, and modifying it to try and figure out what was happening, I finally typed your script on a commandline, putting semi-colons between commands and it worked. So I added semi-colons to your script and then it worked.

Reconnect PSDrives for network connections when running with elevated privileges

$elevated = (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
if( $elevated ) {
write $elevated
#net use | ?{ $_ -match ":\s+\\" -and $.StartsWith("Unavailable") } | %{
net use | ?{ $
.StartsWith("Unavailable") } | %{
$tokens = $.split(":");
$psdrivename = $tokens[0][$tokens[0].length-1];
$path = $tokens[1].trim().split(" ")[0].trim();
if( !(get-psdrive | ?{ $
.Name -eq $psdrivename } )) {
write-host ( "Restoring PSDrive for {0}: {1}" -f $psdrivename, $path );
new-psdrive $psdrivename FileSystem $path | out-null ;
}
}
}

@anderssonjohan
Copy link
Author

@SeeCwriter You don't need semi-colons at the end of the line in PS; Only if you want to do several things on one line. Perhaps you have some bad line-endings or something?

I've hundreds of PS scripts that I use both on mac and windows and never had this issue.

This still works fine (today) with the original script on my win10 VM and it says:

PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17134.858
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17134.858
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

If you want to try to see what's going on and see if PS interprets and runs any of the lines you can do so using Set-PSDebug.

Before you run the script, make sure to execute Set-PSDebug -Trace 2. Don't forget to turn it off when you are done using Set-PSDebug -Off.

Here is an example trace output that is generated that tells what the script/command is doing:

PS> Set-PSDebug -trace 2
PS> write-host "hello"
DEBUG:    1+  >>>> write-host "hello"
DEBUG:     ! CALL function '<ScriptBlock>'
hello
PS> Set-PSDebug -Off
DEBUG:    1+  >>>> Set-PSDebug -Off
DEBUG:     ! CALL function '<ScriptBlock>'

It will also tell about loops and iterations so if it doesn't find any drives to reconnect you would see that there as well.

Good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment