Some PowerShell scripts have to build a temporary drive connection. For example to get a file from a share.
I like to use the “New-PSDrive” commandlet for that. Unfortunately, this commandlet had a bug under Powershell 2.0.
There are, however, alternative solutions.
Index
New-PSDrive in PowerShell 2.0
Why should I use PowerShell 2.0 at all?
Most administrators have probably already switched to either Powershell 3.0 or 4.0 or 5.0, but there are some times when we still have to get back to Powershell 2.0.
I frequently write Powershell scripts for customers. These scripts later run on user-PCs. As Powershell 2.0 is the standard for Windows 7, that’s the reason I am often bound to it.
Credential Parameter
The “New PSDrive” commandlet offers the „Credential“ parameter although it does not work! This can lead to some serious frustration and long searches for a solution – at least this is what I experienced!
More information about the bug on the Microsoft website.
Unfortunately, there is no way to get the Credential parameter working under Powershell 2.0.
Alternative Solutions
Net use
As an alternative for the New-PSDrive, the well-known “net use” can be used in this case. It can be started from a Powershell script without problems.
To avoid conflicts with drive letters, you can run net use without one!
Example:
1 |
net use \\server\share /user:<domain\username> <password> /persisten:no |
For a more detailed explanation about the parameter “/persistent:no” please check my blog article about drive mapping.
The connection can then be addressed via the UNC-path (“\\server\share”). E.g.: \\server\share\folder\file.png
ComObject
You can also employ a ComObject:
1 2 |
$networkObject = New-Object -ComObject WScript.Network $networkObject.MapNetworkDrive("X:","\\server\share",$false, "domain\user", "password") |
“$false” has the same effect as “/persistent:no” with “net use” (see above).
Please contact us about any questions concerning our services.
Leave a Reply
<p>Your email is safe with us.<br/>Information about our <a href="https://activedirectoryfaq.com/contact-us/">data protection policies</a></p>