Doku für PSDrive hinzugefügt
This commit is contained in:
parent
121106cf71
commit
890d87cf6d
@ -0,0 +1,290 @@
|
|||||||
|
New-PSDrive - How to use PowerShell Net Use alternative https://lazyadmin.nl/powershell/new-psdrive/
|
||||||
|
|
||||||
|
New-PSDrive – How to use PowerShell Net Use
|
||||||
|
alternative
|
||||||
|
|
||||||
|
Last updated April 7, 2022 by Rudy Mens
|
||||||
|
|
||||||
|
We all know the Net Use command that we can use to map network drives from the
|
||||||
|
command line. Although we can use Net Use in PowerShell, there is a more powerful
|
||||||
|
alternative, the New-PSDrive cmdlet.
|
||||||
|
|
||||||
|
With the New-PSDrive cmdlet, we cannot only map network drives but also create
|
||||||
|
drive mappings to local folders or registry keys on our computer. Drives created with
|
||||||
|
the cmdlet can be temporary for only the current PowerShell sessions or persistent so
|
||||||
|
they can be used in explorer.
|
||||||
|
|
||||||
|
In this article, we are going to take a closer look at all the possibilities of the New-
|
||||||
|
PSDrive cmdlet with some useful examples for you to use.
|
||||||
|
|
||||||
|
Temporary vs Persistent drives
|
||||||
|
|
||||||
|
Before we are going to take a look at how to create the network drives, I first want to
|
||||||
|
explain the difference between temporary and persistent drives.
|
||||||
|
|
||||||
|
×
|
||||||
|
|
||||||
|
Temporary drives can only be used in the current PowerShell session. They won’t be
|
||||||
|
visible in Windows Explorer and will be gone when you close the PowerShell session.
|
||||||
|
|
||||||
|
1 von 13 20.02.2024, 15:32
|
||||||
|
New-PSDrive - How to use PowerShell Net Use alternative https://lazyadmin.nl/powershell/new-psdrive/
|
||||||
|
|
||||||
|
They are great to use when you need to interact with a remote folder or a long folder
|
||||||
|
path on your local computer.
|
||||||
|
|
||||||
|
Persistent drives are assigned a drive letter and therefore can also be used in Explorer.
|
||||||
|
They will remain available when you close the PowerShell session or even reboot your
|
||||||
|
computer.
|
||||||
|
|
||||||
|
Adding a new network drive with New-PSDrive
|
||||||
|
|
||||||
|
To map a network drive with PowerShell, we are going to create a persistent network
|
||||||
|
connection. This way the network drive will be available in Explorer and other tools
|
||||||
|
and not only in PowerShell.
|
||||||
|
|
||||||
|
There are a couple of required parameters to create a network connection:
|
||||||
|
|
||||||
|
Parameter Description
|
||||||
|
Name Must be an available drive letter
|
||||||
|
PSProvider Set to FileSystem for network shares and folders
|
||||||
|
Root The network location that you want to map
|
||||||
|
To make it available outside PowerShell (in Explorer)
|
||||||
|
×Persist
|
||||||
|
|
||||||
|
2 von 13 20.02.2024, 15:32
|
||||||
|
New-PSDrive - How to use PowerShell Net Use alternative https://lazyadmin.nl/powershell/new-psdrive/
|
||||||
|
|
||||||
|
So to map the network share \\VBoxSvr\Win11 and assign it the drive letter v: we can
|
||||||
|
use the following command:
|
||||||
|
|
||||||
|
1. New-PSDrive -Name V -PSProvider FileSystem -Root \\VBoxSvr\Win11 -Persist Copy
|
||||||
|
|
||||||
|
New-PSDrive network mapping
|
||||||
|
|
||||||
|
The network location will now be available in Explorer or any other tool that you want
|
||||||
|
to use.
|
||||||
|
|
||||||
|
Using different credentials for the mapped network drive
|
||||||
|
|
||||||
|
Just like with Net Use we can use different credentials to open a remote network
|
||||||
|
drive. For this, we first need to create a credential object that we can use to map the
|
||||||
|
|
||||||
|
×network drive.
|
||||||
|
|
||||||
|
3 von 13 Copy
|
||||||
|
|
||||||
|
20.02.2024, 15:32
|
||||||
|
New-PSDrive - How to use PowerShell Net Use alternative https://lazyadmin.nl/powershell/new-psdrive/
|
||||||
|
|
||||||
|
Copy
|
||||||
|
|
||||||
|
1. # Get the credentials
|
||||||
|
|
||||||
|
2. $cred = Get-Credential
|
||||||
|
|
||||||
|
3.
|
||||||
|
|
||||||
|
4. # Create the drive mapping with the credentials
|
||||||
|
|
||||||
|
5. New-PSDrive -Name V -PSProvider FileSystem -Root \\VBoxSvr\Win11 -Persist -
|
||||||
|
Credential $cred
|
||||||
|
|
||||||
|
Creating a Temporary Drive Mapping in PowerShell
|
||||||
|
|
||||||
|
As mentioned we can also create a temporary drive mapping with PowerShell. With
|
||||||
|
temporary mappings, we are not limited to letters only. We can give any name we
|
||||||
|
want to the mapping. Also, we can create a mapping to a local folder on our
|
||||||
|
computer.
|
||||||
|
|
||||||
|
For example, we can create a mapping to our log folder with:
|
||||||
|
|
||||||
|
1. New-PSDrive -Name Log -PSProvider FileSystem -Root c:\temp\logfiles -DescrCiopptiyon
|
||||||
|
|
||||||
|
"Log Folder"
|
||||||
|
|
||||||
|
This way we can quickly navigate to the log files with cd log: . But we can not only
|
||||||
|
navigate to the folder, we now also reference the mapping in other cmdlets. For
|
||||||
|
|
||||||
|
×example:
|
||||||
|
|
||||||
|
4 von 13 20.02.2024, 15:32
|
||||||
|
New-PSDrive - How to use PowerShell Net Use alternative https://lazyadmin.nl/powershell/new-psdrive/
|
||||||
|
1. Get-ChildItem log:
|
||||||
|
Copy
|
||||||
|
|
||||||
|
new-PSDrive
|
||||||
|
|
||||||
|
Creating a persistent mapping to a local folder
|
||||||
|
|
||||||
|
Temporary mappings are gone after you close the PowerShell window. There is no
|
||||||
|
option to store mappings to local folders with New-PSDrive. But when you need to
|
||||||
|
access a long folder path often from PowerShell then you could add the cmdlet to
|
||||||
|
your PowerShell Profile. This way the mapping will be recreated every time you open
|
||||||
|
PowerShell.
|
||||||
|
|
||||||
|
First, open your profile: Copy
|
||||||
|
|
||||||
|
×1. ise $profile 20.02.2024, 15:32
|
||||||
|
|
||||||
|
5 von 13
|
||||||
|
New-PSDrive - How to use PowerShell Net Use alternative https://lazyadmin.nl/powershell/new-psdrive/
|
||||||
|
|
||||||
|
If you get an error that your profile is not found, then follow this article to quickly
|
||||||
|
create one.
|
||||||
|
|
||||||
|
In your PowerShell profile add the following line:
|
||||||
|
|
||||||
|
1. # Create shotcuts to long folder paths Copy
|
||||||
|
|
||||||
|
2. New-PSDrive -Name Log -PSProvider FileSystem -Root c:\temp\logfiles -Description
|
||||||
|
|
||||||
|
"Log Folder" | Out-Null
|
||||||
|
|
||||||
|
Note that we added | Out-Null to the cmdlet. By default, the cmdlet will output the
|
||||||
|
results of the drive mapping. But we don’t want that every time when we open a new
|
||||||
|
PowerShell session.
|
||||||
|
|
||||||
|
Mapping Registry keys with New-PSDrive
|
||||||
|
|
||||||
|
Besides mapping network drives and folders, we can also map registry keys with the
|
||||||
|
New-PSDrive cmdlet. By default, you will find already a mapping to the
|
||||||
|
HKEY_CURRENT_USER key and HKEY_LOCAL_MACHINE key. You can few these with
|
||||||
|
the command:
|
||||||
|
|
||||||
|
1. Get-PsDrive -PSProvider Registry Copy
|
||||||
|
|
||||||
|
2.
|
||||||
|
|
||||||
|
3. # Result
|
||||||
|
|
||||||
|
4. Name Used (GB) Free (GB) Provider Root
|
||||||
|
× CurrentLocation
|
||||||
|
|
||||||
|
6 von 13 20.02.2024, 15:32
|
||||||
|
New-PSDrive - How to use PowerShell Net Use alternative https://lazyadmin.nl/powershell/new-psdrive/
|
||||||
|
|
||||||
|
5. ---- --------- --------- -------- ----
|
||||||
|
HKEY_CURRENT_USER
|
||||||
|
--------------- Registry HKEY_LOCAL_MACHINE
|
||||||
|
Registry
|
||||||
|
6. HKCU
|
||||||
|
|
||||||
|
7. HKLM
|
||||||
|
|
||||||
|
If you want to create a mapping to the HKEY_USERS hives you can do the following:
|
||||||
|
|
||||||
|
1. New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS Copy
|
||||||
|
|
||||||
|
We are not limited to the top-level registry gives, you can create mappings to any
|
||||||
|
registry key you want. For example, the HKCU Windows NT key:
|
||||||
|
|
||||||
|
1. New-PSDrive -Name WinNT -PSProvider Registry -Root Copy
|
||||||
|
HKEY_CURRENT_USER\Software\Microsoft\Windows NT
|
||||||
|
|
||||||
|
You can now easily add, edit or remove registry keys using the create mapping:
|
||||||
|
|
||||||
|
1. # WinNT: is the mapping that we created earlier Copy
|
||||||
|
2. New-Item –Path WinNT: –Name LazyAdmin
|
||||||
|
|
||||||
|
Wrapping Up
|
||||||
|
|
||||||
|
To be honest, there is little advantage to using the New-PSDrive if you only want to
|
||||||
|
map a network share that is also available in Explorer. The Net-Use command is
|
||||||
|
|
||||||
|
×shorter and more convenient to use.
|
||||||
|
|
||||||
|
7 von 13 20.02.2024, 15:32
|
||||||
|
New-PSDrive - How to use PowerShell Net Use alternative https://lazyadmin.nl/powershell/new-psdrive/
|
||||||
|
|
||||||
|
But when you only need to create a mapping in a PowerShell session, or when you
|
||||||
|
want to create a shortcut to local folders or the registry, then the cmdlet can be quite
|
||||||
|
useful.
|
||||||
|
|
||||||
|
If you have any questions, just drop a comment below!
|
||||||
|
|
||||||
|
Did you Liked this Article?
|
||||||
|
|
||||||
|
Get the latest articles like this in your mailbox
|
||||||
|
or share this article
|
||||||
|
|
||||||
|
First name (optional) Email address
|
||||||
|
|
||||||
|
Subscribe
|
||||||
|
|
||||||
|
I hate spam to, so you can unsubscribe at any time.
|
||||||
|
|
||||||
|
× 20.02.2024, 15:32
|
||||||
|
|
||||||
|
You may also like the following articles
|
||||||
|
|
||||||
|
8 von 13
|
||||||
|
New-PSDrive - How to use PowerShell Net Use alternative https://lazyadmin.nl/powershell/new-psdrive/
|
||||||
|
|
||||||
|
Feb 12, 2024
|
||||||
|
|
||||||
|
How to use Connect-MgGraph – All Options
|
||||||
|
|
||||||
|
Feb 5, 2024 20.02.2024, 15:32
|
||||||
|
|
||||||
|
How to Install PowerShell 7 Quickly
|
||||||
|
|
||||||
|
×
|
||||||
|
|
||||||
|
9 von 13
|
||||||
|
New-PSDrive - How to use PowerShell Net Use alternative https://lazyadmin.nl/powershell/new-psdrive/
|
||||||
|
|
||||||
|
Feb 1, 2024
|
||||||
|
|
||||||
|
How to Set an Environment Variable in PowerShell
|
||||||
|
|
||||||
|
Leave a Comment 20.02.2024, 15:32
|
||||||
|
|
||||||
|
×
|
||||||
|
|
||||||
|
10 von 13
|
||||||
|
New-PSDrive - How to use PowerShell Net Use alternative https://lazyadmin.nl/powershell/new-psdrive/
|
||||||
|
|
||||||
|
2. Get MFA Status with Microsoft Entra
|
||||||
|
|
||||||
|
Recommended article
|
||||||
|
|
||||||
|
IT & Office 365 Related
|
||||||
|
1. Best Practice to Secure Office 365 Popular
|
||||||
|
2. Check Domain Controller Health New
|
||||||
|
3. Microsoft Office 365 Plans compared
|
||||||
|
4. Get MFA Status Office 365 users Updated
|
||||||
|
5. Automatically assign licenses in Office 365
|
||||||
|
Home Network
|
||||||
|
1. Best Switches for Home Network New
|
||||||
|
2. UniFi Cloud Controller
|
||||||
|
3. How to set up your home network
|
||||||
|
4. Unifi Access Points Compared Updated
|
||||||
|
5. Unifi Dream Machine Pro Review
|
||||||
|
6. Home Network Wiring Guide
|
||||||
|
Smart Home
|
||||||
|
1. 20 Best Smart Home Gift new
|
||||||
|
2. Getting started with your Smart Home
|
||||||
|
3. Best Smart Home Kit
|
||||||
|
|
||||||
|
×
|
||||||
|
LazyAdmin.nl is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to
|
||||||
|
provide a means for sites to earn advertising fees by advertising and linking to Amazon.com and other sites. LazyAdmin.nl is
|
||||||
|
compensated for referring traffic or business to these companies at no expense to you. Your support helps running this
|
||||||
|
|
||||||
|
12 von 13 20.02.2024, 15:32
|
||||||
|
New-PSDrive - How to use PowerShell Net Use alternative https://lazyadmin.nl/powershell/new-psdrive/
|
||||||
|
|
||||||
|
website and I genuinely appreciate it. I always try to make my reviews, articles and how-to's, unbiased, complete and based
|
||||||
|
on my own expierence.
|
||||||
|
|
||||||
|
Terms and Conditions | Disclaimer | Privacy Policy
|
||||||
|
© 2024 Pixel Supply B.V.
|
||||||
|
|
||||||
|
Update Privacy Preferences
|
||||||
|
A RAPTIVE PARTNER SITE
|
||||||
|
|
||||||
|
× 20.02.2024, 15:32
|
||||||
|
|
||||||
|
13 von 13
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user