How to map drives in powershell

namerg
namerg used Ask the Experts™
on
Hello what is the equivalent of the following:

net use Z: \\computername\c$ /u:computername\username 'password'

in powershell ?

Thanks,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
new-psdrive -name Z -psprovider Filesystem -Root "\\server\folder"

Details on the command:

http://technet.microsoft.com/en-us/library/hh849829.aspx
namergSystems Administrator

Author

Commented:
Hmm, not quite, I want to map toward C$

I get the following error:

new-psdrive : Drive root "\\computername\c$" does not exist or it is not a folder.
At line:1 char:1
+ new-psdrive -name Z -psprovider Filesystem -Root "\\computername\c$"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ReadError: (Z:PSDriveInfo) [New-PSDrive], IOException
    + FullyQualifiedErrorId : DriveRootError,Microsoft.PowerShell.Commands.NewPSDriveCommand

Thanks,
You may want to look at the "credential" section of the link in my comment since you want to map to an admin share.
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

namergSystems Administrator

Author

Commented:
Ok, got it. But how do I avoid the window pop up for the password  ?
you can specify the persist option to keep the drive mapped
namergSystems Administrator

Author

Commented:
I am able to map it but how do i pass the password via powershell script without having that pop up. And, I have to remove the mapped drive because of security. Thanks.
You can pass the password into the script using pscredential

-Credential<PSCredential>

Specifies a user account that has permission to perform this action. The default is the current user.
Type a user name, such as "User01" or "Domain01\User01". Or, enter a PSCredential object, such as one generated by the Get-Credential cmdlet. If you type a user name, you will be prompted for a password.
Beginning in Windows PowerShell 3.0, when the value of the Root parameter is a UNC path, you can use credentials to create file system drives. This parameter is not supported by all Windows PowerShell providers.

You can do some research on that to determine how to proceed.
namergSystems Administrator

Author

Commented:
Got it. Thank You very much.
Np glad to be of some help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial