Rename-LocalUser alternative on PowerShell 4.0

Viewed 97

I know PS 4.0 is ancient. But we're using it anyway on some dev servers it is not my decision.

I'd like to know if there's an alternative for Rename-LocalUser which requires 5.1+ Even if it needs calling wmic, I'm unsure if how to use that kind of call, please give me some examples in one or the other way.

Thank you

1 Answers

Abraham Zinala has provided the right answer in his helpful comments on how to approach this on older PowerShell versions.

You can either use Get-WmiObject to query the local user, and then invoke the .Rename(..) method:

$myAccount = Get-WmiObject Win32_UserAccount -Filter "name='theAccountIWantToRename'"
$myAccount.Rename('myNewUserName')

Or, recommended use of Get-CimInstance and Invoke-CimMethod since WmiObject is no longer available in newer PowerShell versions:

$myAccount = Get-CimInstance Win32_UserAccount -Filter "name='theAccountIWantToRename'"
Invoke-CimMethod -InputObject $myAccount -MethodName Rename -Arguments @{ Name = 'myNewUserName' }
Related