r/PowerShell • u/BamBam-BamBam • Apr 12 '24
"Net use" vs. New-PSDrive
There really are some fundamental differences between "net use" and New-PSDrive and I think it's incorrect to assume that New-PSDrive is a drop-in replacement for "net use."
The use case that I found an issue with is this:
New-PSDrive -Name "X" -PSProvider "FileSystem" -Root \\someserver\someshare
$myDirectory = Get-ChildItem -Path X: -Include "targetdirectory" -Recurse -Force The problem arises when I then try to use the .Root or .FullName property of $myDirectory. They both provide the UNC path instead of
X:\somedirectory\somedirectory...
When mapping X: with "net use," .Root and .FullName return X:\somedirectory...
5
u/waywardcoder Apr 13 '24
I love that psdrives are not restricted to one letter, so I keep od: and src: drives for my OneDrive and source folders, for example. But it is kind of annoying that you need to say (get-item filename) when passing files to native commands or they usually won’t work.
-4
1
u/pigers1986 Apr 13 '24
da faq ? have a look https://i.imgur.com/77PpR9s.png
with new-psdrive it returns expected path - all works fine.
1
u/Tamdjert Mar 07 '25
True. With "net use" you can map a network drive, prompt a user for credentials, save the credentials and make it permanent (persistent) in one line of code. Can't do that with "New-PSDrive".
Sadly "net use" seems not to be able to process variables when executed in Powershell (for example username/password from "Get-Credential").
-1
Apr 12 '24
net use is not Powershell. There is no sense in comparing it to a powershell cmdlet.
-6
u/BamBam-BamBam Apr 12 '24
I disagree.
Apparently, so do these people:
https://lazyadmin.nl/powershell/new-psdrive/
https://www.comptia.org/blog/how-to-map-network-drives
Also, I think it's very important to understand the differences between how native tools behave compared to those in a scripting/programming language.
But, my point was that they behave differently in actual use and that New-PSDrive cannot be considered as a drop-in replacement for "net use." I wasn't declaring one better than the other
Additionally, network drives that are mapped outside of powershell behave like those mapped with "net use." Those mapped by New-PSDrive behave differently than those, as well.
4
Apr 13 '24
I don’t care if you disagree or not. Net use is windows command line. Just because you can use it in powershell does not mean it should be considered when writing powershell.
The only exception I can think of where a powershell equivalent actually doesn’t exist is robocopy.
11
u/devloz1996 Apr 12 '24
I'd understand comparing "net use" to "New-SmbMapping", but not to "New-PSDrive". I mean, the latter can even "mount" registry hives, and anything you can provide PSProvider for.