locked
How to generate random password RRS feed

  • Question

  • Hi Experts,

    i using some cmdlets to generate random password.

    $ascii=$NULL;

    For ($a=97;($a –le 122);$a++) {$ascii+=,[char][byte]$a }

    $No_of_password  = 6
    $length_of_password = 10

    $TempPassword=$NULL     # To store single password
    $Morepassword=@()  # Variable to 'X' no. of passwords

     for ($lp=0; $lp -le $count; $lp++ )
    {

    For ($loop=1; $loop –le $length; $loop++)
    {
        $TempPassword+=($ascii | GET-RANDOM)   # this generate the random password
    }

    $morepassword[$lp]= $TempPassword 

    }

    $morepassword    # This should give the all random password

    Output is like --- 

    =zM;HFuElY
    =zM;HFuElYp88<kqTOVM
    =zM;HFuElYp88<kqTOVMfS1xR01VzY
    =zM;HFuElYp88<kqTOVMfS1xR01VzY4<$wG%Z>ft
    =zM;HFuElYp88<kqTOVMfS1xR01VzY4<$wG%Z>ft=a&ME7>&3T
    =zM;HFuElYp88<kqTOVMfS1xR01VzY4<$wG%Z>ft=a&ME7>&3T$98v$b>jSU

    This is not required output. It should give the output like

    =zM;HFuElY

    p88<kqTOVM

    fS1xR01VzY

    4<$wG%Z>ft

    =a&ME7>&3T

    $98v$b>jSU

    Total 6 passwords with a length of 10

    How do create a array like this ?

    Friday, January 2, 2015 1:08 PM

Answers

  • I agree with Fred but here i shwo to manage two loops with PowerShell.

    $pwdchars=33..122|%{[char]$_}
    
    1..6|
        ForEach-Object{
            $pwd=''
            1..10 | 
                ForEach-Object{
                    $pwd+=$pwdchars | GET-RANDOM
                }
            $pwd
    }
         

    It is usually always better to write less code and to use the code correctly.  YOu have guessed at mmost of the code but have not used consistent variables.  My guess is that you have tried to modify somecode you found.  Look at this code to see how easy it is to build loops in PowerShell


    ¯\_(ツ)_/¯



    • Edited by jrv Friday, January 2, 2015 2:58 PM
    • Marked as answer by Amitkumar_Patil_India Monday, January 5, 2015 6:15 AM
    Friday, January 2, 2015 2:53 PM

All replies

  • Generating passwords such as 4<$wG%Z>ft is counterproductive. Since few people can remember such a string they will invariably jot it down on paper, thus defying the purpose of having a secure password.
    Friday, January 2, 2015 1:16 PM
  • I agree with Fred but here i shwo to manage two loops with PowerShell.

    $pwdchars=33..122|%{[char]$_}
    
    1..6|
        ForEach-Object{
            $pwd=''
            1..10 | 
                ForEach-Object{
                    $pwd+=$pwdchars | GET-RANDOM
                }
            $pwd
    }
         

    It is usually always better to write less code and to use the code correctly.  YOu have guessed at mmost of the code but have not used consistent variables.  My guess is that you have tried to modify somecode you found.  Look at this code to see how easy it is to build loops in PowerShell


    ¯\_(ツ)_/¯



    • Edited by jrv Friday, January 2, 2015 2:58 PM
    • Marked as answer by Amitkumar_Patil_India Monday, January 5, 2015 6:15 AM
    Friday, January 2, 2015 2:53 PM
  • We can also do discontinuous arrays of characters like this:

    ([char]'!'..[char]'+')+([char]'A'..[char]'Z')+([char]'a'..[char]'z')|%{[char]$_}


    ¯\_(ツ)_/¯

    Friday, January 2, 2015 3:08 PM
  • No need to reinvent the wheel.

    Windows IT Pro: Generating Random Passwords in PowerShell


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Saturday, January 3, 2015 7:24 PM
    Friday, January 2, 2015 3:13 PM
  • Thanks Experts,

    It has solved my issue.

    Monday, January 5, 2015 6:15 AM
  • thanks JRV and Bill  i have marked it as answer.
    Monday, January 5, 2015 6:15 AM