Mapping Network Shares Using PowerShell

Good morning,

I am trying to create a PowerShell script for mapping a network share to Windows 7 workstations via GPO.

The mapping needs to map the share so that when the student logs into the Windows 7 workstation, they will have their network share mapped on their system. The location which I mapping consists of several network shares (1 share for every student which is accessible using their domain user credentials).

The following is what I have tried using to test map the share to my test system:

New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\networkshare" -Persist

I tried running this command just for my user and I receive the following errors:

How can I get this to work just for my user and how can I configure the script so that it will use the domain credentials of the logged on user? In other words, how can I configure this script so that the correct share is mapped for each user that logs into the system?

Thanks in advance,

February 10th, 2015 8:30pm

If you need drive mappings, use group policy rather than a script.
Free Windows Admin Tool Kit Click here and download it now
February 10th, 2015 8:49pm

Why wouldn't you do this entirely with a GPO? https://technet.microsoft.com/en-ca/library/cc770902.aspx

Am I misunderstanding your goal?

February 10th, 2015 8:49pm

I agree with the others: Use Group Policy and Group Policy Preference (GPP) to map your network drives. Consider that with GPP, you can use Item-Level Targeting--it's amazing. That said, here are some other thoughts I had before I questioned why you'd use PS this way:

- Can you map the drive using net use, such as net use t: \\server\share
- Are you using a valid UNC: \\server\share? Your example indicates you've only included the server portion of your UNC path.

Free Windows Admin Tool Kit Click here and download it now
February 10th, 2015 8:56pm

Thanks for your reply guys.

I am using a valid UNC: New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\server\share" -Persist, and I am receiving the error.

I have tried mapping via GPO but it does not work, with or without Item-Level targeting, and it does not map on my test system, which is on the domain.

I did create a batch script using net use T: \\server\share\ /Persistent:yes but it does not work. Receiving System error 53: the network path was not found (after entering credentials). The network path exists just fine and is accessible when manually mapping to it.

I tried using the same net use command via PowerShell and nothing got mapped. But when I use the same command by providing the name of the share: net use t: \\server\share\my share /persistent:yes, then the command works and does not prompt me for credentials.

Is there a command I can use in my PowerShell script or the batch script that will use the credentials of whichever user is logging into the system and by doing so, it will map that users share? The whole point for this will be to apply this via GPO so that whichever user logs into the system, they will have their share mapped which is located in \\server\share\.

Thanks in advance,

 

February 10th, 2015 10:30pm

No script required if you go with this:

https://support.microsoft.com/kb/816313?wa=wsignin1.0

Extremely simple to set up, this is what I use.

Free Windows Admin Tool Kit Click here and download it now
February 10th, 2015 10:59pm

Hi Mike,

Thanks for the suggestion and link but that is not what I am trying to accomplish.

The \\server\share\ already consists of network shares for each user. I need to be able to map  the network server share \\server\share\ via GPO or script so that whichever user logs into the system to which the GPO has been applied to, they will be able to see their share mapped as a network drive. 

Each users share is located in \\server\shares\

Thanks,

February 10th, 2015 11:27pm

Thanks for your reply guys.

I am using a valid UNC: New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\server\share" -Persist, and I am receiving the error.

I have tried mapping via GPO but it does not work, with or without Item-Level targeting, and it does not map on my test system, which is on the domain.

I did create a batch script using net use T: \\server\share\ /Persistent:yes but it does not work. Receiving System error 53: the network path was not found (after entering credentials). The network path exists just fine and is accessible when manually mapping to it.

I tried using the same net use command via PowerShell and nothing got mapped. But when I use the same command by providing the name of the share: net use t: \\server\share\my share /persistent:yes, then the command works and does not prompt me for credentials.

Is there a command I can use in my PowerShell script or the batch script that will use the credentials of whichever user is logging into the system and by doing so, it will map that users share? The whole point for this will be to apply this via GPO so that whichever user logs into the system, they will have their share mapped which is located in \\server\share\.

Thanks in advance,

 

You have to resolve the 53 error. That is not a scripting issue. You are using a share path the does not exist.  Fix that and you are set.

I suspect you are miing an error message somewhere.

Free Windows Admin Tool Kit Click here and download it now
February 10th, 2015 11:30pm

Hi Mike,

Thanks for the suggestion and link but that is not what I am trying to accomplish.

The \\server\share\ already consists of network shares for each user. I need to be able to map  the network server share \\server\share\ via GPO or script so that whichever user logs into the system to which the GPO has been applied to, they will be able to see their share mapped as a network drive. 

Each users share is located in \\server\shares\

Thanks,

That's pretty much what the home folder does.

Connect driveLetter: To: \\servername\users\%username%

Now each user will have the drive letter you specify connected automatically. If the folder doesn't exist, ADUC will create it for you.

Perhaps I'm not clear on what you're trying to accomplish.

February 10th, 2015 11:37pm

Thanks for your reply guys.

I am using a valid UNC: New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\server\share" -Persist, and I am receiving the error.

I have tried mapping via GPO but it does not work, with or without Item-Level targeting, and it does not map on my test system, which is on the domain.

I did create a batch script using net use T: \\server\share\ /Persistent:yes but it does not work. Receiving System error 53: the network path was not found (after entering credentials). The network path exists just fine and is accessible when manually mapping to it.

I tried using the same net use command via PowerShell and nothing got mapped. But when I use the same command by providing the name of the share: net use t: \\server\share\my share /persistent:yes, then the command works and does not prompt me for credentials.

Is there a command I can use in my PowerShell script or the batch script that will use the credentials of whichever user is logging into the system and by doing so, it will map that users share? The whole point for this will be to apply this via GPO so that whichever user logs into the system, they will have their share mapped which is located in \\server\share\.

Thanks in advance,

 

You have to resolve the 53 error. That is not a scripting issue. You are using a share path the does not exist.  Fix that and you are set.

I suspect you are miing an error message somewhere.

Free Windows Admin Tool Kit Click here and download it now
February 11th, 2015 12:34am

Thanks for your reply guys.

I am using a valid UNC: New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\server\share" -Persist, and I am receiving the error.

I have tried mapping via GPO but it does not work, with or without Item-Level targeting, and it does not map on my test system, which is on the domain.

I did create a batch script using net use T: \\server\share\ /Persistent:yes but it does not work. Receiving System error 53: the network path was not found (after entering credentials). The network path exists just fine and is accessible when manually mapping to it.

I tried using the same net use command via PowerShell and nothing got mapped. But when I use the same command by providing the name of the share: net use t: \\server\share\my share /persistent:yes, then the command works and does not prompt me for credentials.

Is there a command I can use in my PowerShell script or the batch script that will use the credentials of whichever user is logging into the system and by doing so, it will map that users share? The whole point for this will be to apply this via GPO so that whichever user logs into the system, they will have their share mapped which is located in \\server\share\.

Thanks in advance,

 

You have to resolve the 53 error. That is not a scripting issue. You are using a share path the does not exist.  Fix that and you are set.

I suspect you are miing an error message somewhere.

February 11th, 2015 12:35am

This is another case where you told us the wrong info. None of your posts have been consistent and the image is unreadable. Please try to b more careful in the future and you will get better answers.

New-PSDrive -Name T -PSProvider FileSystem -Root \\server\share\mysharename -Persist

That will always work if this works:
net use t: \\server\share\mysharename

Note that I don't decorate everything with lots of quotes and I use the exact same root UNC.

Free Windows Admin Tool Kit Click here and download it now
February 11th, 2015 1:22am

jrv,

If you can, please read my post from the very beginning. I have provided the same information throughout this post and provided the same examples throughout.

This being said... the PowerShell command does not work because it returns the following error message:

New-PSDrive : The specified network resource or device is no longer available                                              At line:1 char:1                                                                                                                                  + New-PSDrive "T" -PSProvider FileSystem -Root "\\server\share\%username%"...                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo: InvalidOperation: (T:PSDRiveInfo) [New-PSDrive], Win32Exception                              + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand

Thanks,

I am entering the command like this: 

New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\server\share\%username%" -Persist


February 11th, 2015 1:58am

You cannot say %username% in PowerShell.

You must say $Env:USERNAME instead.

Note that it applies to the current user only.

Free Windows Admin Tool Kit Click here and download it now
February 11th, 2015 2:09am

Just wondering,  why don't you do this under the user properties in Active Directory?  There is a spot to map a home folder,  and it uses the UNC you are specifying. 
February 11th, 2015 2:09am

jrv,

If you can, please read my post from the very beginning. I have provided the same information throughout this post and provided the same examples throughout.

This being said... the PowerShell command does not work because it returns the following error message:

New-PSDrive : The specified network resource or device is no longer available                                              At line:1 char:1                                                                                                                                  + New-PSDrive "T" -PSProvider FileSystem -Root "\\server\share\%username%"...                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo: InvalidOperation: (T:PSDRiveInfo) [New-PSDrive], Win32Exception                              + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand

Thanks,

I am entering the command like this: 

New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\server\share\%username%" -Persist


You keep changing the command. Anyone knows the %username% does not work in PowerShell. 

You need to learn how to ask a question in a technical forum.

Please review this: http://mikepope.com/blog/DisplayBlog.aspx?permalink=554

Free Windows Admin Tool Kit Click here and download it now
February 11th, 2015 2:10am

Thanks jrv for your comments.

Thank you to all for your help!

February 15th, 2015 8:04pm

Thanks jrv for your comments.

Thank you to all for your help!

Free Windows Admin Tool Kit Click here and download it now
February 16th, 2015 4:01am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics