Access an existing mapped drive

Hi

Using PS 4.O. I am using a mapped ‘home’ drive (T:) to store my scripts so that they are backed up nightly. When I set my default location to this drive using Set-Location T:\Scripts cmdlet I get the following:

Set-Location : Cannot find path 'T:\Scripts' because it does not exist. At C:\Users\Tony.Wainwright\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1:1 char:1 + Set-Location T:\Scripts + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (T:\Scripts:String) [Set-Location], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

Looking through the help I have found the New-PSDrive cmdlet this sorts out my problem:

New-PSDrive -Name T -PSProvider FileSystem -Root "\\Lincoln\Data\Tony Wainwright" Set-Location T:\Scripts

However this now shows the output of the New-PSDrive. Is there a way I can pipe the New-PSDrive to Set-Location?

Thanks
Tony

To get it not to show the output of the New-PSDrive you could just pipe it to Out-Null, so you’d get:

New-PSDrive -Name T -PSProvider FileSystem -Root "\\Lincoln\Data\Tony Wainwright" | Out-Null
Set-Location T:\Scripts

If you relly want to pipe it, Set-Location does accept the Path parameter by name, so you could do:

New-PSDrive -Name T -PSProvider FileSystem -Root "\\Lincoln\Data\Tony Wainwright" | Select @{N="Path";E={"$($_.Name):\Scripts"}} | Set-Location

Thanks Robert just what I need.

What’s the difference between the 2 in terms of adherence to best practice? I’m trying to learn PS and know that there’s now right or wrong way as long as it works. I just don’t want to get into any bad habits at the start.

I can’t speak for best practices or not, but if you ask me the Select makes the code harder to read. I’d probably do something like:

$newDrive = New-PSDrive -Name T -PSProvider FileSystem -Root "\\Lincoln\Data\Tony Wainwright"
Set-Location "$($newDrive.Name):\Scripts"

This is easy to read and the Set-Location will still take the drive name from the return value of the New-PSDrive cmdlet.

Thanks. I agree that reading it is easier