r/PowerShell • • 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...

0 Upvotes

12 comments sorted by

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.

-17

u/BamBam-BamBam Apr 12 '24

Sure, but that wasn't my point.

10

u/vermyx Apr 13 '24

But the point still stands. What you state is comparing new-psdrive to net use is the same as comparing apples and oranges. They fundamentally work differently. I can map a drive in one command prompt via net use and it will be available in another one if it was already opened. New-psdrive won’t because powershell maps aliases when it starts to a: to z: and manupulates the for the session.

2

u/budtske Apr 14 '24 edited Apr 14 '24

it was never intended to be a drop in replacement so the problem is with your question itself

If I read your question more as: hey I can't use Fullname or root when I do this: here is suppose. But then your question was very weirdly phrased.

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

u/BamBam-BamBam Apr 13 '24

No argument on new-psdrive's utility.

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

u/[deleted] 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

u/[deleted] 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.