r/PowerShell • u/AzureSkye • Nov 18 '25
Solved Why is "net use" so much faster than "Get-SmbMapping"??
I'm trying to retrieve the paths of the user's mapped network drives to save/transfer to the user's new computer. This is part of a user transfer script I'm working on. The only thing I need is the remote path for remapping.
I've found that "net use" returns info in ~50 milliseconds, while "Get-SmbMapping" can take 5-30 seconds to get the same info.
Out of sheer curiosity: why is there such a difference? Am I using "Get-SmbMapping" wrong?
56
Upvotes
22
u/phatcat09 Nov 18 '25
Everyone is saying the same thing so I'll throw my version: Powershell is designed for maximum utility not performance, but you can still use utilities that are.
Whatever it's cool!
Just be thankful you can with little effort.
33
u/Semt-x Nov 18 '25
Get-SMBMapping talks to WMI, that can be really slow
Is this faster and still gives the desired result?
get-psdrive -PSProvider FileSystem | ?{$_.DisplayRoot -match '^\\'}4
u/Thotaz Nov 18 '25
When we are talking several seconds like that, it's likely due to some timeout issue.
Get-SmbMappingis either doing more things under the hood where it's experiencing a timeout, or it ends up calling a different native API with different timeout behavior thannet usedoes.At a high level, I'd assume
net usehas some fairly simple argument parsing and logic before simply calling the relevant win32 functions (probably https://learn.microsoft.com/en-us/windows/win32/api/lmshare/nf-lmshare-netshareenum ).Get-SmbMappingis a lot more complicated. It's a cdxml module defined in XML so that XML has to be parsed and processed in PowerShell. PowerShell converts this to PowerShell functions which then has to be processed into C# expression trees like any other PowerShell code would.This then calls WMI which does some black magic and ultimately ends up calling the same, or similar native Win32 APIs to get the relevant data.
But again, while those layers do take some performance, they are not the reason why it's this much slower.
1
u/AzureSkye Nov 19 '25
Thank you all for the information! I ended up writing a Compare Commands function to loop and measure each option. Reading directly from the registry was the absolute fastest, with pure "net use" usually running second.
For reference, here are the commands I tried:
8
u/odwulf Nov 18 '25
If you compare Powershell and Solution X, Powershell will usually be slower. That's the price of near universality and ease of use. Here it's about wrapping up WMI, but any time, you always have to wrap up .NET. .NET is slow, and wrapping it up is even slower. You have to learn to juggle with lower level accesses if you need to optimize speed. I once had to optimize a script that was going through a huge network folder and using a dry run of robocopy followed by direct .NET calls, I went down from 7 or 8 hours (!) to less than ten minutes. That's how slow non-speed-optimized things can be in PS.
4
u/ITjoeschmo Nov 18 '25
IIRC mapped drives are stored in the user registry. May be simpler to just grab the keys from there
2
u/tenebot Nov 18 '25
Perhaps the delay is due to WMI init voodoo? Per this article, try disabling TCP (or decreasing the timeout).
2
u/Sabinno Nov 18 '25
The “net” command was released in 1985. The performance was designed for use in DOS.
1
u/ka-splam Nov 19 '25
Run
Get-Command Get-SmbMapping | Format-Listyou can see the definition of it mentioning the CDXML wrappers /u/Thotaz said. You can also see the output is a particular WMI class and could maybe skip the wrapping and go to that with:Run SysInternals' strings on net.exe and there are Windows API names like
NetUseEnumandWNetOpenEnumWwhich is probably possible for someone with the low level skills to wrap in C# P/Invoke andAdd-Typeinto PowerShell and skip the CIM/WMI layer, too. And the PowerShell object creation.Simpler to call
net.exethough.-2
u/KE3JU Nov 18 '25
Because the .NET Framework is so horribly inefficient.
0
u/[deleted] Nov 18 '25
[deleted] — view deleted comment