How do I properly create a random password or initial password when I creating an local admin account using PS

PerserPolis-1732 1,326 Reputation points
2022-02-14T14:12:04.497+00:00

Hi,

I have a script creating new local user account . Now I want to create a random password Or a initial password when I create a new local user account

that is the script

$UserLogon = Get-WinEvent -LogName 'Microsoft-Windows-User Profile Service/Operational' `
| Where-Object {$_.Id -eq 2} | Select-Object -First 1

$LastUser = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$($UserLogon.UserId.Value)" -Name "ProfileImagePath"
$substringIndex = $LastUser.ProfileImagePath.LastIndexOf("\") + 1
$LastUserName = $LastUser.ProfileImagePath.Substring($substringIndex)
$a=$LastUserName+"Admin"
New-LocalUser -AccountNeverExpires:$true -Password ( ConvertTo-SecureString -AsPlainText -Force 'password') -Name $a -FullName "Local Administrator" -Description "Local Administrator" | Add-LocalGroupMember -Group administrators

regards

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,118 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
{count} votes

Accepted answer
  1. Andreas Baumgarten 96,266 Reputation points MVP
    2022-02-16T14:48:04.197+00:00

    Hi @PerserPolis-1732 ,

    remove this line:

    $result = $a + "," + $password  
    

    and add these lines instead:

    $hostname = $Env:COMPUTERNAME  
    $result = $hostname + "," + $a + "," + $password  
    

    As this thread is getting a little messy, maybe you should open a new question if there is something else you need.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


4 additional answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2022-02-14T15:59:09.257+00:00

    Here's a password generator that works pretty well:

    Function GeneratePassword {
        [CmdletBinding()]
        param(
            [Parameter(ValueFromPipeline=$false)]
                [ValidateRange(1,256)]
                [int]$PWLength = 16,
            [Parameter(ValueFromPipeline=$false)]
                [char[]]$CharSet1 = 'abcdefghiklmnoprstuvwxyz'.ToCharArray(),
            [Parameter(ValueFromPipeline=$false)]
                [char[]]$CharSet2 = 'ABCDEFGHKLMNOPRSTUVWXYZ'.ToCharArray(),
            [Parameter(ValueFromPipeline=$false)]
                [char[]]$CharSet3 = '1234567890'.ToCharArray(),
            [Parameter(ValueFromPipeline=$false)]
                [char[]]$CharSet4 = '!"$%&/()=?}][{@#*+'.ToCharArray(),
            [Parameter(ValueFromPipeline=$false)]
                [ValidateRange(0,256)]
                [int]$Set1Num = 8,
            [Parameter(ValueFromPipeline=$false)]
                [ValidateRange(0,256)]
                [int]$Set2Num = 3,
            [Parameter(ValueFromPipeline=$false)]
                [ValidateRange(0,256)]
                [int]$Set3Num = 2,
            [Parameter(ValueFromPipeline=$false)]
                [ValidateRange(0,256)]
                [int]$Set4Num = 3
        )
    
            $password =  $CharSet1 | Get-Random -Count $Set1Num
            $password += $CharSet2 | Get-Random -Count $Set2Num
            $password += $CharSet3 | Get-Random -Count $Set3Num
            $password += $CharSet4 | Get-Random -Count $Set4Num
    
            Write-Verbose "Initial password: $(-join $password)"
    
            $Password = -join ($password | Get-Random -Count $PWLength)
    
            Write-Verbose "Final   password: $Password"
    
            Write-Output $password
    }
    
    <#
    $LcAlpha = 'abcdefghiklmnoprstuvwxyz'.ToCharArray()
    $UcAlpha = 'ABCDEFGHKLMNOPRSTUVWXYZ'.ToCharArray()
    $Numbers = '1234567890'.ToCharArray()
    $NonAlphaNum = '!"$%&/()=?}][{@#*+'.ToCharArray()
    
    # 8 character password, 5 lower-case, 1 upper-case, 1 digit, 1 punctuation
    GeneratePassword -Verbose 8 $LcAlpha $UcAlpha $Numbers $NonAlphaNum 5 1 1 1
    
    # 16 character password, 8 lower-case, 3 upper-case, 2 digit, 3 punctuation
    GeneratePassword -Verbose
    #>
    
    1 person found this answer helpful.

  2. PerserPolis-1732 1,326 Reputation points
    2022-02-14T14:52:49.9+00:00

    Hi sok,

    Sorry how to get the password?

    Regards


  3. Andreas Baumgarten 96,266 Reputation points MVP
    2022-02-14T16:09:37.627+00:00

    Hi @PerserPolis-1732 ,

    Maybe this helps:
    https://github.com/abaumgarten42/Useful_PSscripts/blob/main/Generate-RandomPassword/Generate-RandomPassword.ps1

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  4. PerserPolis-1732 1,326 Reputation points
    2022-02-16T12:47:15.723+00:00

    Hi Andres,

    the script works now fine.

    Thanks a lot

    0 comments No comments