Archive

Posts Tagged ‘PowerShell’

Creating local users – en mass

December 30th, 2009 No comments

The traditional way of creating a vast amount of users would include a lot of time as well as patience. Or – maybe just a few lines of (magic) PowerShell code.

So first of all, we have to connect to the local ADSI. This can be done via WinNT://[ComputerName]:

$ComputerName = $env:COMPUTERNAME
$Computer = [adsi]"WinNT://$ComputerName"

This is already the most crucial part. Next we create a new user, set the username, fullname and description and the password.

function Add-User([string]$UserName, [string]$FullName, [string]$Description, [string]$Password)
{
    $User = $Computer.Create("user", $UserName)

    $User.Put("fullName", $FullName)
    $User.Put("description", $Description)
    $User.SetPassword($Password)

    $User.SetInfo()
}

The SetInfo() call ensures, that the data is actually persisted.

Voila, that’s already all you need to get going. Well, in order to create a massive amount of users you will want to create a csv-file of users like:

UserName,FullName,Description,Password
usera,User A,Desc A,passworda
userb,User B,Desc B,passwordb

This can be read with just a single line of code.

function Import-Users([string]$UserFile)
{
    Import-Csv $UserFile | foreach-object { Add-User $_.UserName $_.FullName $_.Description $_.Password }
}

So now all you need is to call Import-Users myNewUsers.csv from the PowerShell command-line and off you go!

Categories: Programming, Windows Tags:

When "save" feels like lockdown

June 24th, 2009 No comments

So Microsoft really got down to business with this security thing – after installing PowerShell on my new machine I figured, that it’s configured in restricted mode.

This is not some kind of limited mode of operation, but rather this denies the execution of all scripts whatsoever – the term restricted suggested something else, at least in my mindset.

The four PowerShell execution policies include the following:

  • Restricted: No scripts can run.
  • AllSigned: Ps1 and .Ps1xml files must be digitally signed.
  • RemoteSigned: Ps1 and .Ps1xml files from the internet must be digitally signed.
  • Unrestricted: No digital signatures are required and all scripts can be executed.

To find your current policy-setting just use Get-ExecutionPolicy.

Categories: General Tags: