Keeping mapped network drives in an elevated Powershell session

Seems that there’s a bit of a gotcha when using powershell as an elevated user: any mapped network drives won’t be available, unless you explicitly connect them. I used to have the mapped drives I use hard-coded into my profile, but whenever the IT department move things around it breaks, and then I get error messages whenever I log in as an elevated user and have to remember what they mean.

So this is my workaround; a pair of scripts that lists all the mapped drives used by the regular user, then loads them up for the elevated user. The first is this script:

Get-PSDrive -PSProvider FileSystem |
Select-Object Name, DisplayRoot |
Where-Object {$_.DisplayRoot -ne $null}|
Export-Clixml "~\appdata\local\mappedDrives.xml"

This uses powershell to get all the mapped network drives and export the list as an xml file. I schedule it to run whenever I log in.

The first script makes an XML file in the %Appdata% folder with a list of the drive paths and their letters, so all you have to do is import it and mount them whenever you elevate your session. To do this I have this in my profile.ps1:

# check to see if user is elevated
If (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")){ 
  # import the data
  $mappedDrives = Import-Clixml "~\appdata\local\mappedDrives.xml"
  for ($i=0; $i -lt $mappedDrives.length; $i++){
    $d = $mappedDrives[$i];
    $n = $ + ":";
    $r = $d.DisplayRoot;
    #mount each drive
    if (! (test-Path $n)){
      net use "$n" "$r";


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.