LocalAccount.psm1
<#
.Synopsis Creates a local user account in the Targeted computername .DESCRIPTION Creates a local user account in the Targeted computername .EXAMPLE NEW-Localuser -Name TestUser1 -Computername RemotePC1 -Password 'password123' -Description 'A new User' #> function New-LocalUser { [CmdletBinding()] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]]$Name, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)] [string[]]$Computername = "$Env:computername", [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)] [ValidateScript({$_.GetType().Name -eq 'SecureString'})] [array][system.management.automation.pscredential]$Password, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=3)] [string[]]$Description=' ' ) Begin { } Process { $Plaintextpassword=$password[0].GetNetworkCredential().password $computer = [ADSI]"WinNT://$($ComputerName[0]),computer" $user = $computer.Create("User", "$($Name[0])") $user.setpassword("$PlainTextPassword") $user.put("Description",$($Description[0])) $user.SetInfo() } End { } } <# .Synopsis Creates a local group in the Targeted computername .DESCRIPTION Creates a local group in the Targeted computername .EXAMPLE NEW-Localgroup -name TestUser1 -Computername RemotePC1 -Description 'A new group' #> function New-LocalGroup { [CmdletBinding()] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]]$name, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)] [string[]]$Computername = "$Env:computername", [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)] [string[]]$Description ) Begin { } Process { $computer = [ADSI]"WinNT://$($ComputerName[0]),computer" $group = $computer.Create("Group", $name[0]) $group.SetInfo() $group.description=$Description[0] $group.SetInfo() } End { } } <# .Synopsis Gets a list of local users in the Targeted computername .DESCRIPTION Gets a list of local users in the Targeted computername .EXAMPLE Get-Localuser -computername remotepc1 #> function Get-LocalUser { [CmdletBinding()] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)] [string[]]$Computername = "$Env:computername" ) Begin { } Process { $computer = [ADSI]"WinNT://$($ComputerName[0]),computer" $computer.psbase.Children | where { $_.psbase.schemaclassname -match 'user' }` | Select-Object -property ` @{Name='Name';Expression= { $_.name }},` @{Name='Fullname';Expression= { $_.Fullname }},` @{Name='Description';Expression= { $_.Description }},` @{Name='AutoUnlockInterval';Expression= { $_.AutoUnlockInterval }},` @{Name='BadPasswordAttempts';Expression= { $_.BadPasswordAttempts }},` @{Name='HomeDirDrive';Expression= { $_.HomeDirDrive }},` @{Name='HomeDirectory';Expression= { $_.HomeDirectory }},` @{Name='LastLogin';Expression= { $_.LastLogin }},` @{Name='LockoutObservationInterval';Expression= { $_.LockoutObservationInterval }},` @{Name='LoginHours';Expression= { $_.LoginHours }},` @{Name='LoginScript';Expression= { $_.LoginScript }},` @{Name='MaxBadPasswordsAllowed';Expression= { $_.MaxBadPasswordsAllowed }},` @{Name='MaxPasswordAge';Expression= { $_.MaxPasswordAge }},` @{Name='MaxStorage';Expression= { $_.MaxStorage }},` @{Name='MinPasswordAge';Expression= { $_.MinPasswordAge }},` @{Name='MinPasswordLength';Expression= { $_.MinPasswordLength }},` @{Name='objectSid';Expression= { $_.objectSid }},` @{Name='Parameters';Expression= { $_.Parameters }},` @{Name='PasswordAge';Expression= { $_.PasswordAge }},` @{Name='PasswordExpired';Expression= { $_.PasswordExpired }},` @{Name='PasswordHistoryLength';Expression= { $_.PasswordHistoryLength }},` @{Name='PrimaryGroupID';Expression= { $_.PrimaryGroupID }},` @{Name='Profile';Expression= { $_.Profile }},` @{Name='UserFlags';Expression= { $_.UserFlags }} } End { } } <# .Synopsis Gets a list of local groups in the Targeted computername .DESCRIPTION Gets a list of local groups in the Targeted computername .EXAMPLE Get-Localuser -computername remotepc1 #> function Get-LocalGroup { [CmdletBinding()] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)] [string[]]$Computername = "$Env:computername" ) Begin { } Process { $computer = [ADSI]"WinNT://$($ComputerName[0]),computer" $computer.psbase.Children | where { $_.psbase.schemaclassname -match 'group' } | ` Select-Object -property ` @{Name='Name';Expression= { $_.name }},` @{Name='Description';Expression= { $_.Description }},` @{Name='objectSid';Expression= { $_.objectSid }} } End { } } function Remove-LocalGroup { [CmdletBinding(SupportsShouldProcess=$true)] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]]$name, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)] [string[]]$Computername = "$Env:computername" ) Begin { } Process { if ($PSCmdlet.Shouldprocess("$name Removed from $($computername[0])") ) { $computer = [ADSI]"WinNT://$($ComputerName[0]),computer" $computer.delete("group",$name[0]) } } End { } } function Remove-LocalUser { [CmdletBinding(SupportsShouldProcess=$true)] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]]$Name, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)] [string[]]$Computername = "$Env:computername" ) Begin { } Process { if ($PSCmdlet.Shouldprocess("$Name Removed from $computername") ) { $computer = [ADSI]"WinNT://$($ComputerName[0]),computer" $computer.delete("user",$name[0]) } } End { } } function Rename-LocalUser { [CmdletBinding()] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]]$Name, [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=1)] [string[]]$NewName, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=2)] [string[]]$Computername="$ENV:Computername" ) Begin { } Process { $user = [ADSI]"WinNT://$($computername[0])/$($name[0]),user" $user.psbase.rename($newname[0]) } End { } } function Rename-LocalGroup { [CmdletBinding()] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]]$Name, [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=1)] [string[]]$NewName, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=2)] [string[]]$Computername="$ENV:Computername" ) Begin { } Process { $group = [ADSI]"WinNT://$($computername[0])/$($name[0]),group" $group.psbase.rename($newname[0]) } End { } } function Disable-LocalUser { [CmdletBinding(SupportsShouldProcess=$true)] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]]$Name, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)] [string[]]$Computername="$ENV:Computername" ) Begin { } Process { if ($PSCmdlet.Shouldprocess("$Name Disabled on $computername") ) { $user = [ADSI]"WinNT://$($computername[0])/$($Name[0]),user" $status = $user.userflags $Disable=[int]$Status.tostring() -bxor 512 -bor 2 $user.userflags=$disable $user.setinfo() } } End { } } function Enable-LocalUser { [CmdletBinding()] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]]$Name, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)] [string[]]$Computername="$ENV:Computername" ) Begin { } Process { $user = [ADSI]"WinNT://$($computername[0])/$($Name[0]),user" $status = $user.userflags $Enable=[int]$Status.tostring() -bxor 2 -bor 512 $user.userflags=$enable $user.setinfo() } End { } } function Add-LocalGroupMember { [CmdletBinding()] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]]$GroupName, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)] [string[]]$Computername = "$Env:computername", [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)] [string[]]$name ) Begin { } Process { $group = [ADSI]"WinNT://$($computername[0])/$($groupname[0]),group" $group.add("WinNT://$($Name[0]),user") } End { } } function Remove-LocalGroupMember { [CmdletBinding(SupportsShouldProcess=$true)] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]]$GroupName, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)] [string[]]$Computername = "$Env:computername", [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)] [string[]]$name ) Begin { } Process { if ($PSCmdlet.Shouldprocess("$($Name[0]) Removed from $($groupname[0]) on $computername") ) { $group = [ADSI]"WinNT://$($computername[0])/$($groupname[0]),group" $group.remove("WinNT://$($Name[0]),user") } } End { } } function Get-LocalGroupMember { [CmdletBinding()] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]]$Name, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=2)] [string[]]$Computername="$ENV:Computername" ) Begin { } Process { # Code for decoding group membership provided # Courtesy of Francois-Xaver Cat # Windows PowerShell MVP # Thanks Dude! $group = [ADSI]"WinNT://$($computername[0])/$($Name[0]),group" $group.psbase.invoke("members") | ForEach-Object { $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)} } End { } } Export-ModuleMember -Function * |