Thursday, October 1, 2009

Create user and manage Windows services with PowerShell

Ok, so in all, I've got a couple scripts to share here. A couple posts ago a presented the fact that I had forgotten about a local user account that needed to be created on loaner laptops so that the auto login feature could be setup. At the time I couldn't figure out how to do this with PowerShell, but of course, I've figured something out. Below are two scripts, one creates a local user on the machine it's ran on called User. It doesn't create any password for the account. The second script adds that local account called User to the local Administrators group.

Create Local User
Clear-Host
$localuser = "User"
$password = ""
$description = "Auto Logon Account"

$computer = [ADSI]"WinNT://$env:computername,computer"
$user = $computer.Create("User", $localuser)
$user.SetPassword($password)
$user.SetInfo()
$user.Description = [String] $description
$user.SetInfo() 

Add to local group
 $sysname = gwmi -q "Select Name from Win32_ComputerSystem"
$admgrp = [ADSI]”WinNT://$($sysname.Name)/Administrators,group”
$admgrp.add("WinNT://$($sysname.Name)/user")

So after I got those scripts together and added them to my task sequence and deployed it to a laptop, I found another thing I missed. (Note that I'm only missing these things because instead of using an image based deployment, I'm installing the operating system from scratch so alot of customization has to be done afterward.) The Security Center in Windows XP was active. Normally when building an image I would have just gone in and  unchecked the three check boxes in the Security Center that relate to alerting me about things, but when looking up how to do this through the registry or through PowerShell I discovered that you can simply turn off the Security Center service and disable the entire thing. Below is the script for that.

Disable Security Center
Stop-Service "Security Center"
Set-Service -name wscsvc -startuptype disabled

Enjoy

No comments: