Username into UNC path

This topic contains 6 replies, has 5 voices, and was last updated by  Rob Simmers 1 month ago.

  • Author
  • #102653


    I want help to create a powershellscript that insert the username into an UNC-path, and create a mapped networkdrive. The username must be the login user. At the end of the username, I would like to add $ It's a shared folder for the user logged in. Just like this:
    Thank you in advance for all suggestions!

  • #102659

    Olaf Soyk

    What kind of help do you need? You know that you can (should) use group policies to connect network drives in user sessions, don't you?

  • #102661


    Group policie is not an option here. If that was an option, I would not ask for help to powershell.

  • #102662

    I Like Cheese

    In PowerShell you can get the currently logged in user with the built in environment by typing at the prompt $env:USERNAME which you can do a search for further detailed information.
    So to put your path into a string you could do something like:

    $SharePath = "\\servername\$env:USERNAME`$"
    • #102670


      Worked like a charm. Thank you so much "I like chees". This was spot on. Just what I needed!:-)

  • #102664


    Simply, pull the user names from AD or text file or DB or type them is as a inscript string array.
    Append the '$' symbol to the end of the name using string concatenation as well as any other needed string addition.
    This is really PowerShell 101 stuff. Check out the YouTube vids on Beginning PowerShell or the MS Virtual Academy training on PowerShell or So the PowerShell help file covers thus in more detail than I am showing here.
    So, a simple example (well, excluding all the drive mapping and folder creation steps), would be:

    ForEach ($Username in  (Get-ADUser -Filter *).SamAccountName)
    {"\\servername\$username" + '$'}
    # Results

    You'd simply modify the above with the additional steps needed to complete your use case.
    Or visit the MS and grab a more detailed script from there and tweak it for your needs

    PowerShell script to create home folder for Active Directory users
    This PowerShell script creates a home (Personal) folder for all users in Active Directory and automatically configures folder permission to ensure that a user's folder can only be accessed by the user. Also maps the folder to a drive when user logs on to the domain. Customizable..


  • #102722

    Rob Simmers

    Another option using string format:

    $ServerName = "Server123"
    $SharePath = '\\{0}\{1}$' -f $ServerName, $env:USERNAME

You must be logged in to reply to this topic.