Map a network drive based on the user group

Dear everyone,


I know there is a tone of topics about that but i need a script a bit specific.

I need in Powershell to map a network drive based on the group user.

The particularity is : There is not a predifined groupe like "Finance". There is a group for the "CLIENT" that could be "group_client_0001" and this group have 2 members "user_0001" and "user_0002". Those users must have a drive mapped with :

 for user 1 : Z:\group_client_0001\user_0001

 for user 2 : Z:\group_client_0001\user_0002

The next group could be "group_client_0002" and this group could have 1 member "user_0003"

The user will have a drive mapped :

 for user 3 : Z:\group_client_0002\user_0003


any ideas ?

  • So each user is a member of just a single group using the group_client_000x naming convention or is it also possible that a user is a member of multiple groups like that.

    Also how would you like to run this script, as a logon script?

  • You are right the user could have multiple group

    so i could use another area like company :


    $searcher = [adsisearcher]"(samaccountname=$env:username)"
    $company = $searcher.FindOne()
    IF($company) {           
        $(New-Object -ComObject WScript.Network).RemoveNetworkDrive("U:");
        $(New-Object -ComObject WScript.Network).MapNetworkDrive("U:", "\\srvdata1\donnees1\"+$company+"\"+$env:username);           
    yes it will be in a logon script
  • You could indeed do that, I would change the following piece of your code though:

    $(New-Object -ComObject WScript.Network).RemoveNetworkDrive("U:");
    $(New-Object -ComObject WScript.Network).MapNetworkDrive("U:", "\\srvdata1\donnees1\"+$company+"\"+$env:username);

    To this:

    (New-Object -ComObject WScript.Network).RemoveNetworkDrive("U:")
    (New-Object -ComObject WScript.Network).MapNetworkDrive("U:","\\srvdata1\donnees1\$company\$($env:username)")

    So what I have changed, and why, is the following:

    1. Removed the $- in front of the brackets, this is not required. Just placing brackets are new-object is enough
    2. Removed the semi-*** at the end of the line. This is not required in PowerShell, some programming languages do require this but we are lucky enough to be scripting so this is not required for us.
    3. When mounting the network drive you were building the string using concatenation. PowerShell is quite smart about this so it is possible to put most variables directly in the quotes. What I have changed though is added $() around the environment variable because we are selecting a specific property of the environment variables
    I hope this all makes sense, let me know if it helps. 

  • Ok, thank you Jaap.

    I have another question about this script, how i could get this variable in powershell (sorry french) :


    I would like to do :

    $searcher = [adsisearcher]"(samaccountname=$env:username)"

    $company = $searcher.FindOne()

    IF($company) {            

    (New-Object -ComObject WScript.Network).RemoveNetworkDrive("U:")

    (New-Object -ComObject WScript.Network).MapNetworkDrive("U:","__THE_VARIABLE_I_NEED__\$($env:username)")           


  • My french is a bit rusty, but I would say you are looking for the homedirectory property. For more information about this property have a look at the following msdn article:

    As for code you could use to retrieve this property, something along these lines should suffice:

  • Ok i modified the script, i think it's good : 

    $searcher = [adsisearcher]"(samaccountname=$env:username)"

    $homedirectory = $searcher.FindOne().Properties.homedirectory

    $HomeDriveLetter = "U:" 

    $DriveletterExists = Test-Path -Path $HomeDriveLetter


    IF($homedirectory) {            

        If (-not ($DriveletterExists)) {

             (New-Object -ComObject WScript.Network).MapNetworkDrive("U:","$homedirectory\$($env:username)")




    Do you know a command to rename the maped network drive without UNC information ? Like "Your Datas"

  • Sure you could use the following one-liner to rename a mapped network drive:

    (New-Object -com Shell.Application).NameSpace('U:').Self.Name = 'Your Data'