Username into UNC path

Welcome Forums General PowerShell Q&A Username into UNC path

This topic contains 6 replies, has 5 voices, and was last updated by

 
Participant
4 months ago.

  • Author
    Posts
  • #102653

    Participant
    Points: 0
    Rank: Member

    Hi.
    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:
    \\servername\username$
    Thank you in advance for all suggestions!

  • #102659

    Participant
    Points: 8
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

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

  • #102662

    Participant
    Points: 7
    Rank: Member

    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

      Participant
      Points: 0
      Rank: Member

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

  • #102664

    Participant
    Points: 16
    Rank: Member

    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
    \\servername\Administrator$
    \\servername\Guest$
    ...
    \\servername\krbtgt$
    ...
    \\servername\user002$
    \\servername\user003$
    \\servername\user001$
    \\servername\user004$
    ...
    

    You'd simply modify the above with the additional steps needed to complete your use case.
    Or visit the MS PowerShellGallery.com 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..

    CreatehomefolderforADUsers.zip

    'gallery.technet.microsoft.com/scriptcenter/PowerShell-script-to-832e08ed'

  • #102722

    Participant
    Points: 1
    Rank: Member

    Another option using string format:

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

The topic ‘Username into UNC path’ is closed to new replies.