none
Create password in powershell for local admin account

    Question

  • If I have an existing local user account called scott. In PowerShell what is the command to create a password called password. This would need to be able to be run on any computer so can not be computer name specific.
    Friday, August 17, 2012 5:59 PM

Answers

  • Well, I did some research and I think you can get an object of type System.DirectoryServices.DirectoryEntry that can represent the local account.  This object seems to implement the IADsUser interface, meaning you should be able to call the SetPassword() method on it.

    After having a quick loot at http://blogs.technet.com/b/heyscriptingguy/archive/2010/11/22/use-powershell-to-enable-or-disable-a-local-user-account.aspx, my PowerShell attempt would look like this:

    # Read the username from the console or assign directly, whatever:
    $username = "scott";
    # Or:
    $username = Read-Host "Type the username:";
    $user = [ADSI]"WinNT://./$username";
    $user.SetPassword("TheNewPasswordGoesHere");


    Jose R. MCP
    Code Samples

    Friday, August 17, 2012 10:20 PM
  • Alternatively...

    net user <username> <password>

    The Powershell session must be started by an administrator.

    Check the change with

    net user <username>

    before and after the password set.

    Tested on Win7 Home with Powershell 2


    Just a humble SysAdmin

    • Marked as answer by Scott26 Monday, August 20, 2012 4:21 PM
    Friday, August 17, 2012 11:28 PM

All replies

  • Well, I did some research and I think you can get an object of type System.DirectoryServices.DirectoryEntry that can represent the local account.  This object seems to implement the IADsUser interface, meaning you should be able to call the SetPassword() method on it.

    After having a quick loot at http://blogs.technet.com/b/heyscriptingguy/archive/2010/11/22/use-powershell-to-enable-or-disable-a-local-user-account.aspx, my PowerShell attempt would look like this:

    # Read the username from the console or assign directly, whatever:
    $username = "scott";
    # Or:
    $username = Read-Host "Type the username:";
    $user = [ADSI]"WinNT://./$username";
    $user.SetPassword("TheNewPasswordGoesHere");


    Jose R. MCP
    Code Samples

    Friday, August 17, 2012 10:20 PM
  • Alternatively...

    net user <username> <password>

    The Powershell session must be started by an administrator.

    Check the change with

    net user <username>

    before and after the password set.

    Tested on Win7 Home with Powershell 2


    Just a humble SysAdmin

    • Marked as answer by Scott26 Monday, August 20, 2012 4:21 PM
    Friday, August 17, 2012 11:28 PM