Answered by:
change local admin password

Question
-
Good morning!
I'm trying to create a short script to change the local admin account password to use a word which will then be followed by the last 7 digits of the computer name but without the
example: computer name: WS-STC-3070-003
password to be changed to : password3070003
This is what I have managed to put together:
Import-Module ActiveDirectory $password = "test" + $ENV:COMPUTERNAME.Substring($ENV:COMPUTERNAME.Length - 8) Get-LocalUser –Name “admin” | Set-LocalUser –Password $password
I keep getting the following error:
Set-LocalUser : Cannot bind parameter 'Password'. Cannot convert the "test3070-003" value of type "System.String" to type "System.Security.SecureString". At C:\Users\localuser\Desktop\testing admin password.ps1:6 char:55 + Get-LocalUser –Name “admin” | Set-LocalUser –Password $password + ~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Set-LocalUser], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.SetLocalUserCommand
Can you give me some tips please.
Thursday, January 23, 2020 11:02 AM
Answers
-
Hey,
That should be pretty easy. You can do:
$password = "test" + $ENV:COMPUTERNAME.Substring($ENV:COMPUTERNAME.Length - 8) $password = $password -replace '-'
and then go and create your secure string (as already discussed):
$secureString = ConvertTo-SecureString $password -AsPlainText -Force Get-LocalUser –Name “admin” | Set-LocalUser –Password $secureString
So if your Computer name is 'WS-STC-3070-003', it will be reduced to the desired lenght first and then the '-' will be removed. You can reverse the oprations of course - First remove the dashes and do a 'Substring' afterwards.
I hope I could help. Regards,
(Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov
- Marked as answer by LiviuSob Thursday, January 23, 2020 2:13 PM
Thursday, January 23, 2020 12:23 PM
All replies
-
Hi,
you will have to convert the password into something you can pass (Secure String):
$password = "test" + $ENV:COMPUTERNAME.Substring($ENV:COMPUTERNAME.Length - 8) $secureString = ConvertTo-SecureString $password -AsPlainText -Force
and then use the Securte String like that:
Get-LocalUser –Name “admin” | Set-LocalUser –Password $secureString
Hope I could help.
Regards,
(Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov
- Edited by Stoyan ChalakovMVP Thursday, January 23, 2020 11:20 AM
Thursday, January 23, 2020 11:18 AM -
Thank you very much.
That did work.
Any idea how to remove the dashes and use only the numbers?
Thursday, January 23, 2020 11:30 AM -
Hey,
That should be pretty easy. You can do:
$password = "test" + $ENV:COMPUTERNAME.Substring($ENV:COMPUTERNAME.Length - 8) $password = $password -replace '-'
and then go and create your secure string (as already discussed):
$secureString = ConvertTo-SecureString $password -AsPlainText -Force Get-LocalUser –Name “admin” | Set-LocalUser –Password $secureString
So if your Computer name is 'WS-STC-3070-003', it will be reduced to the desired lenght first and then the '-' will be removed. You can reverse the oprations of course - First remove the dashes and do a 'Substring' afterwards.
I hope I could help. Regards,
(Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov
- Marked as answer by LiviuSob Thursday, January 23, 2020 2:13 PM
Thursday, January 23, 2020 12:23 PM -
Bless you!
Gold Star from me!
Thursday, January 23, 2020 2:14 PM