Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"net use" in PowerShell without specifying drive

Tags:

powershell

With "net use", you could do the following:

net use \\server /user:domian\username

It would then prompt for a password, and any further CIFS connections made to that server using any program (cmd, Explorer, Word, etc) would automatically use that credential.

Is there an equivalent way of doing this in PowerShell? I know New-PsDrive usually provides the answer for "net use", but it requires a drive letter to map:

PS C:\WINDOWS\system32> New-PsDrive -PSProvider FileSystem -Root \\server -Credential domain\user
cmdlet New-PSDrive at command pipeline position 1
Supply values for the following parameters:
Name: 
New-PSDrive : Cannot bind argument to parameter 'Name' because it is an empty string.
At line:1 char:1
+ New-PsDrive -PSProvider FileSystem -Root \\server -Credential domain ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [New-PSDrive], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.NewPSDriveCommand

I don't want to map a drive, I just want to say "when I connect to this server, use this credential". Is there a way?

like image 441
DarkMoon Avatar asked Oct 12 '16 22:10

DarkMoon


People also ask

Can I use net use in PowerShell?

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.

Is net use persistent by default?

The net use command has a parameter called /persistent that defines whether or not a connection remains after a reboot. Mapped drives are not persistent, by default. Windows will remember the last created connection's persistence setting.

How do you use net use?

To view information about a connection, you can do either of the following: Type net use DeviceName to get information about a specific connection. Type net use to get a list of all the computer's connections.


1 Answers

The cmdlet New-SMBMapping is (by and large) functionally equivalent to New-PSDrive. However, the equivalent parameter -LocalPath (-Name in New-PSDrive) is not required so you shouldn't get the above error.

like image 188
InfosecSapper Avatar answered Oct 05 '22 23:10

InfosecSapper