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'})] $Password, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=3)] [string]$Description=' ' ) Begin { } Process { $ConvertCred=New-object System.Management.Automation.PSCredential -ArgumentList ' ',$Password $Plaintextpassword=$ConvertCred.GetNetworkCredential().password $computer = [ADSI]"WinNT://$ComputerName,computer" $user = $computer.Create("User", "$Name") $user.setpassword("$PlainTextPassword") $user.put("Description",$Description) $user.SetInfo() } End { } } <# .Synopsis Creates a local group in the Targeted computername .DESCRIPTION Creates a local group in the Targeted computername .EXAMPLE NEW-Localgroup -groupname TestUser1 -Computername RemotePC1 -Description 'A new group' #> function New-LocalGroup { [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]$Description ) Begin { } Process { $computer = [ADSI]"WinNT://$ComputerName,computer" $group = $computer.Create("Group", "$Groupname") $group.SetInfo() $group.description=$Description $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=0)] [string]$Name, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)] [string]$Computername = "$Env:computername", [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=2)] [string]$Description ) Begin { } Process { $computer = [ADSI]"WinNT://$ComputerName,computer" $computer.psbase.Children | where { $_.psbase.schemaclassname -match 'user' } | Select-Object -property @{Name='Name';Expression= { $_.name }},@{Name='Fullname';Expression= { $_.Fullname }},@{Name='Description';Expression= { $_.Description }} } 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=0)] [string]$GroupName, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)] [string]$Computername = "$Env:computername", [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=2)] [string]$Description ) Begin { } Process { $computer = [ADSI]"WinNT://$ComputerName,computer" $computer.psbase.Children | where { $_.psbase.schemaclassname -match 'group' } | Select-Object -property @{Name='Name';Expression= { $_.name }},@{Name='Description';Expression= { $_.Description }} } End { } } function Remove-LocalGroup { [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" ) Begin { } Process { if ($PSCmdlet.Shouldprocess("$Groupname Removed from $computername") ) { $computer = [ADSI]"WinNT://$ComputerName,computer" $computer.delete("group",$groupname) } } 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,computer" $computer.delete("user",$name) } } 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=$true, ValueFromPipelineByPropertyName=$true, Position=2)] [string]$Computername ) Begin { } Process { $user = [ADSI]"WinNT://$computername/$oldname,user" $user.psbase.rename("$newname") } 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=$true, ValueFromPipelineByPropertyName=$true, Position=2)] [string]$Computername ) Begin { } Process { $group = [ADSI]"WinNT://$computername/$oldname,group" $group.psbase.rename("$newname") } End { } } function Disable-LocalUser { [CmdletBinding(SupportsShouldProcess=$true)] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string]$Name, [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=1)] [string]$Computername ) Begin { } Process { if ($PSCmdlet.Shouldprocess("$Name Disabled on $computername") ) { $user = [ADSI]"WinNT://$computername/$Name,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=$true, ValueFromPipelineByPropertyName=$true, Position=1)] [string]$Computername ) Begin { } Process { $user = [ADSI]"WinNT://$computername/$Name,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]$Name, [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)] [string]$Computername = "$Env:computername", [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)] [string]$Groupname ) Begin { } Process { $group = [ADSI]"WinNT://$computername/$groupname,group" $group.add("WinNT://$Name,user") } End { } } function Remove-LocalGroupMember { [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", [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)] [string]$Groupname ) Begin { } Process { if ($PSCmdlet.Shouldprocess("$Name Removed from $Groupname on $computername") ) { $group = [ADSI]"WinNT://$computername/$groupname,group" $group.remove("WinNT://$Name,user") } } End { } } Export-ModuleMember -Function * |