PoshCode Archive  Artifact Content

Artifact b3e701d194c54d1bd2197e428ce34a920c4a3d7ee5bd56ccae67a8d2946c40a6:

  • File Set-LocalPassword.ps1 — part of check-in [b9e2e7bea9] at 2018-06-10 13:55:16 on branch trunk — Sets local account passwords on one or more computers. (user: Nathan Hartley size: 1604)

# encoding: ascii
# api: powershell
# title: Set-LocalPassword
# description: Sets local account passwords on one or more computers.
# version: 0.1
# type: function
# author: Nathan Hartley
# license: CC0
# x-poshcode-id: 543
# x-derived-from-id: 547
# x-archived: 2016-06-21T17:28:25
# x-published: 2009-08-21T15:14:00
#
# [computerName1,computerName2,... | ] ./Set-LocalPassword.ps1 [-user] <userName> [-password] <password> [[-computers] computerName1,computerName2,...]
#
# Sets local account passwords on one or more computers
# usage [computerName1,computerName2,... | ] ./Set-LocalPassword.ps1 [-user] <userName> [-password] <password> [[-computers] computerName1,computerName2,...]

param(
	[string] $User
	, [string] $Password
    , [string[]] $ComputerNames = @()
)

$ComputerNames += @($input)

if (! $ComputerNames)
{
    $ComputerNames = $env:computername
}


function ChangePassword ([string] $ChangeComputer, [string] $ChangeUser, [string] $ChangePassword) {
	"*** Setting password for $ChangeComputer/$ChangeUser"
	& {
	$ErrorActionPreference="silentlycontinue"
	([ADSI] "WinNT://$computer/$user").SetPassword($password)
	if ($?) { " Success" }
	else { " Failed: $($error[0])" }
	}

}

function ShowUsage {
@'
usage [computerName1,computerName2,... | ] ./Set-LocalPassword.ps1 [-user] <userName> [-password] <password> [[-computers] computerName1,computerName2,...]
'@
}


if ($user -match '^$|-\?|/\?|-help' -or ! $password) { ShowUsage; break; }
ForEach ($computer in $ComputerNames) { 
	ChangePassword $computer $user $password 
}