I am currently creating a script for auto creation of AD account in Powershell. I created the script all worked but I can’t seems to be able to find out what the password is set to , is there any way I can view the Password generated in exported txt file for example just so i can test if this actually works fine before i deploy, You can see i have wrote to display as Plain Text but still doesnt show what pass has been generated. this is the code bellow<\/p>\n
Function New-RandomPassword([Int]$Size = 14,[Char<\/span>]$CharSets=“ULNS”,[Char<\/span>]$Exclude) foreach ($dom in $myDomains) #Create<\/span> new ad user I would try something like this. Then you can view $password to see it. Also, you are misunderstsanding what -asplaintext does. It is converting from plain text to a secure string not the other way around.<\/p>\n I am currently creating a script for auto creation of AD account in Powershell. I created the script all worked but I can’t seems to be able to find out what the password is set to , is there any way I can view the Password generated in exported txt file for example just so i can test if this actually works fine before i deploy, You can see i have wrote to display as Plain Text but still doesnt show what pass has been generated. this is the code bellow<\/p>\n Function New-RandomPassword([Int]$Size = 14,[Char<\/span>]$CharSets=“ULNS”,[Char<\/span>]$Exclude) foreach ($dom in $myDomains) #Create<\/span> new ad user Thank you, this seems to work fine, but I have another issue now this is the error I get<\/p>\n ConvertTo-SecureString : Cannot bind argument to parameter ‘String’ because it is null. Hrm, I’m not receiving an error on my end. Are you using Powershell ISE? If so, close it completely and reopen it to make sure nothing is squirrely with the variables.<\/p>","upvoteCount":0,"datePublished":"2021-05-20T08:05:13.000Z","url":"https://community.spiceworks.com/t/how-to-display-securestring-password-in-powershell/800422/4","author":{"@type":"Person","name":"jrp78","url":"https://community.spiceworks.com/u/jrp78"}},{"@type":"Answer","text":" My fault I saw something I missed, thank you very much !<\/p>","upvoteCount":1,"datePublished":"2021-05-20T08:32:57.000Z","url":"https://community.spiceworks.com/t/how-to-display-securestring-password-in-powershell/800422/5","author":{"@type":"Person","name":"spiceuser-n1fbn","url":"https://community.spiceworks.com/u/spiceuser-n1fbn"}},{"@type":"Answer","text":" Going forward<\/p>\n If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>
\n{
\n$Chars=@();$TokenSet=@()
\nIf(!$TokenSets){$Global:TokenSets=@{
\nU=[Char<\/span>]‘ABCDEFGHJKLMNPQRSTUVWXYZ’ #Upper<\/span> case
\nL=[Char<\/span>]‘abcdefghijkmnpqrstuvwxyz’ #Lower<\/span> case
\nN=[Char<\/span>]‘1234567890’ #Numerals<\/span>
\nS=[Char<\/span>]‘!@$!@$!@$!@$’ #Symbols<\/span>
\n}}
\n$CharSets|ForEach-Object{
\n$Tokens=$TokenSets.“$\"|ForEach-Object{If($Exclude-cNotContains$<\/em>){$}}
\nIf($Tokens){
\n$TokensSet+=$Tokens
\n#Character<\/span> sets defined in upper case are mandatory
\nIf($<\/em>-cle[Char]“Z”){$Chars+=$Tokens|Get-Random}
\n}
\n}
\nWhile($Chars.Count-lt$Size){$Chars+=$TokensSet|Get-Random}
\n#Mix<\/span> the (mandatory) characters and output string
\n($Chars|Sort-Object{Get-Random})-Join”\"
\n}
\nImport-Module ActiveDirectory
\n$myDomains = “”<\/p>\n
\n{
\n#Securing<\/span> the Password and converting to plain text
\n$password = ConvertTo-SecureString -String $(New-RandomPassword -length 14) -AsPlainText -Force<\/p>\n
\nNew-ADUser -server $dom<\/code>
\n-Name “” -GivenName \"\"<\/code>
\n-Surname “” -SamAccountName \"\"<\/code>
\n-UserPrincipalName “” -EmailAddress \"\"<\/code>
\n-Organization “” -Description \"\"<\/code>
\n-Department “” -Company \"\"<\/code>
\n-Manager “” -Path \",OU=,OU=Sites,DC=,DC=\"<\/code>
\n-AccountPassword $password
\n}<\/p>","upvoteCount":3,"answerCount":6,"datePublished":"2021-05-20T07:25:11.000Z","author":{"@type":"Person","name":"spiceuser-n1fbn","url":"https://community.spiceworks.com/u/spiceuser-n1fbn"},"acceptedAnswer":{"@type":"Answer","text":"Function New-RandomPassword([Int]$Size = 14,[Char[]]$CharSets=\"ULNS\",[Char[]]$Exclude)\n{\n $Chars=@();$TokenSet=@()\n If(!$TokenSets){$Global:TokenSets=@{\n U=[Char[]]'ABCDEFGHJKLMNPQRSTUVWXYZ' #Upper case\n L=[Char[]]'abcdefghijkmnpqrstuvwxyz' #Lower case\n N=[Char[]]'1234567890' #Numerals\n S=[Char[]]'!@$!@$!@$!@$' #Symbols\n }}\n $CharSets|ForEach-Object{\n $Tokens=$TokenSets.\"$_\"|ForEach-Object{If($Exclude-cNotContains$_){$_}}\n If($Tokens){\n $TokensSet+=$Tokens \n #Character sets defined in upper case are mandatory\n If($_-cle[Char]\"Z\"){$Chars+=$Tokens|Get-Random} \n }\n }\n While($Chars.Count-lt$Size){$Chars+=$TokensSet|Get-Random}\n #Mix the (mandatory) characters and output string\n ($Chars|Sort-Object{Get-Random})-Join\"\"\n}\nImport-Module ActiveDirectory\n$myDomains = \"\"\n\nforeach ($dom in $myDomains)\n{\n #Generate password\n $password = New-RandomPassword -length 14\n #Securing the Password\n $encryptpw = ConvertTo-SecureString -String $password -AsPlainText -Force\n\n #Create new ad user\n New-ADUser `\n -server $dom `\n -Name \"\" `\n -GivenName \"\" `\n -Surname \"\" `\n -SamAccountName \"\" `\n -UserPrincipalName \"\" `\n -EmailAddress \"\" `\n -Organization \"\" `\n -Description \"\" `\n -Department \"\" `\n -Company \"\" `\n -Manager \"\" `\n -Path \",OU=,OU=Sites,DC=,DC=\" `\n -AccountPassword $encryptpw\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-05-20T07:39:23.000Z","url":"https://community.spiceworks.com/t/how-to-display-securestring-password-in-powershell/800422/2","author":{"@type":"Person","name":"jrp78","url":"https://community.spiceworks.com/u/jrp78"}},"suggestedAnswer":[{"@type":"Answer","text":"
\n{
\n$Chars=@();$TokenSet=@()
\nIf(!$TokenSets){$Global:TokenSets=@{
\nU=[Char<\/span>]‘ABCDEFGHJKLMNPQRSTUVWXYZ’ #Upper<\/span> case
\nL=[Char<\/span>]‘abcdefghijkmnpqrstuvwxyz’ #Lower<\/span> case
\nN=[Char<\/span>]‘1234567890’ #Numerals<\/span>
\nS=[Char<\/span>]‘!@$!@$!@$!@$’ #Symbols<\/span>
\n}}
\n$CharSets|ForEach-Object{
\n$Tokens=$TokenSets.“$\"|ForEach-Object{If($Exclude-cNotContains$<\/em>){$}}
\nIf($Tokens){
\n$TokensSet+=$Tokens
\n#Character<\/span> sets defined in upper case are mandatory
\nIf($<\/em>-cle[Char]“Z”){$Chars+=$Tokens|Get-Random}
\n}
\n}
\nWhile($Chars.Count-lt$Size){$Chars+=$TokensSet|Get-Random}
\n#Mix<\/span> the (mandatory) characters and output string
\n($Chars|Sort-Object{Get-Random})-Join”\"
\n}
\nImport-Module ActiveDirectory
\n$myDomains = “”<\/p>\n
\n{
\n#Securing<\/span> the Password and converting to plain text
\n$password = ConvertTo-SecureString -String $(New-RandomPassword -length 14) -AsPlainText -Force<\/p>\n
\nNew-ADUser -server $dom<\/code>
\n-Name “” -GivenName \"\"<\/code>
\n-Surname “” -SamAccountName \"\"<\/code>
\n-UserPrincipalName “” -EmailAddress \"\"<\/code>
\n-Organization “” -Description \"\"<\/code>
\n-Department “” -Company \"\"<\/code>
\n-Manager “” -Path \",OU=,OU=Sites,DC=,DC=\"<\/code>
\n-AccountPassword $password
\n}<\/p>","upvoteCount":3,"datePublished":"2021-05-20T07:25:11.000Z","url":"https://community.spiceworks.com/t/how-to-display-securestring-password-in-powershell/800422/1","author":{"@type":"Person","name":"spiceuser-n1fbn","url":"https://community.spiceworks.com/u/spiceuser-n1fbn"}},{"@type":"Answer","text":"
\nAt line:29 char:49<\/p>\n\n
<\/code><\/pre>\n<\/li>\n
[ConvertTo-SecureString], ParameterBindingValidationException<\/li>\n