locked
%username conversion to correct SamAccountName. RRS feed

  • Question

  • Dear Community,

    I am currently creating a script to ease user creation on Windows Server 2019 ST.

    its a very basic script i got from the internet and created my own.

    New-ADuser -Name "" -DisplayName "" -HomeDrive "H:" -HomeDirectory "\\Shares\%USERNAME% -GivenName "" -Surname "" -SamAccountName "test" -UserPrincipalName "" -Path "OU=Users,OU=Active Users,DC=Domain,DC=local" -AccountPassword(Read-Host -AsSecureString "Input Password") -Enabled $true

    this script works almost 100% the only problem i am facing is that when using the HomeDirectory parameter with the %username% input it does not convert this to the samAccountName automatically. it just inputs it as soon.

    with the little language knowledge i have i know everything between "" is seen as a string so it writes it down as seen.

    But upon running the script i would like it to recognize it and convert the %username% to test.

    Thank you in advanced and will kindly wait for any suggestions.

    Wednesday, February 13, 2019 2:56 PM

All replies

  • %USERNAME% is for cmd prompt, PowerShell use $env:USERNAME

    "\\Shares\$($env:USERNAME)"


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Wednesday, February 13, 2019 3:24 PM edited
    Wednesday, February 13, 2019 3:23 PM
  • When i use your suggestion. it creates or sets the homefolder name as administrator. my bad not writing what i wanted to use as the naming convention for the homefolder. The homefolder name should be equal to the sAMAccountName.

    if i have a user that his/her username is ptest. i want the home folder to get the same name as the SamAccountName.

    Wednesday, February 13, 2019 5:18 PM
  • It is because $env:USERNAME will return the currently logged in user. Since you are using the New-ADUser cmdlet, why not just use the same data that you are using on the -sAmAccountName parameter?

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Proposed as answer by jrv Wednesday, February 13, 2019 8:30 PM
    Wednesday, February 13, 2019 7:14 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Thursday, February 28, 2019 2:23 PM