Modify local users property description in Powershell 4.0

443 Views Asked by At

I would like to modify the description property from local users. I can retrieve all local users name and description in two ways.

One using WMI Win32_UserAccount class:

Get-CimInstance -ClassName Win32_UserAccount -Filter "LocalAccount='True'" | Select-Object -Property name, description | FL

And one using ADSI:

$Computername = $env:COMPUTERNAME
$adsi = [ADSI]"WinNT://$Computername"
$Users = $adsi.Children | Where-Object {$_.SchemaClassName -eq 'user'}
ForEach ($u in $Users) {
    $u | Select-Object -Property Name, Description
}

However, I can't figure out how using commands similar to the ones above I can modify the local user property description. I did research and only found out how to do GETS and not SETS. My final objective is to put this code in an Ansible playbook and run it in several remote servers. If you have an idea of how to solve this or how to help me I would be grateful.

1

There are 1 best solutions below

0
On BEST ANSWER

Working with ADSI can be very tricky but, its super helpful since it usually doesnt rely on 3rd party modules.

Without going super in depth on ADSI, heres the easiest way you can change, or add a value to a property, the description property in this case:

$adsi = [ADSI]"WinNT://$env:Computername"
$User = $adsi.Children.Find('Abraham')
$User.Description = "Hi, this is a description"
$User.SetInfo()

Using $User.SetInfo method, we can write the changes to the database. It's not a method you would get either when piping to a Get-Member. Unfortunately, it's one that you would need to know already. Using Dot Notation we can reference the property you'd like to change, then assigning it a value just like we would when assigning a value to a variable: $var = value.