CRM 2011 – Why PowerShell is Great

Posted on May 17, 2011


I noticed that CRM 2011 had powershell shipped with it.  My initial understanding of power shell is it was similar to DOS/Command/shell window and it allowed you to write scripts to automate tasks.

The few times I have had to write DOS scripts it has been a struggle with lots of googling trying to work out how to do anything

then I read this, powershell is like a mix of shell and scripting language

here is a link to some videos which will give you a great introduction to powershell

The reason I was looking at powershell was from a great blog post from the mcscrm blogger called creating users in active directory using powershell

Here included a powershell script and from reading his blog and looking at this I thought how much easier it would be writing a powershell script rather than just shell.  It was the addition of the if and for loops that really opened my eyes to the potential of powershell.  I’m sure many people reading this blog post have already realised that powershell is an extremely useful tool

$domain = (get-addomain).distinguishedname
$path = "OU=Employees,$domain"
$employees = Get-ADObject -Filter {distinguishedname -eq $path}
If ( -not $employees) {
  $employees = $domain.Create("OrganizationalUnit", "ou=Employees")

$users = import-csv "C:\Scripts\usersToBeCreated.csv"
$ldappath = "LDAP://$path"
$container = [ADSI] $ldappath
$users | foreach {
    $first = $_.FirstName
    $last = $_.LastName
    $username = "$first" + "." + "$last"
    $email = "$username" + "@demo.local"
    $newUser = $container.Create("User", "cn=" + $username)
    $newUser.Put("sAMAccountName", $username)
    $newUser.Put("description","Demo Account")
    $newUser.psbase.InvokeSet('AccountDisabled', $false)
About these ads
Posted in: CRM 2011, PowerShell