I’ve been meaning to learn PowerShell for some time now as its an important of SharePoint 2010 and I thought I better get started. In SharePoint 2010 even if you wanted to try and avoid PowerShell you won’t be able to as this is the best way to patch your farm.
Over the years I have created several batch scripts from collecting computer programs and services installed on all machines on a network, creating all my default Active Directory accounts and a user creation script that adds user to the correct Organisation Unit and AD Group which then allows the user to access the correct sites within SharePoint. I love these scripts and I love batch files as they have always done what I wanted them to do at the time. But PowerShell is here now and its a new learning curve and I need to learn it.
I’ve played around with it a little before when I’ve mailbox enabled an Active Directory username and it has returned the PowerShell command but that’s cheating as it has already given me the code. I’m not going to learn it by copying and pasting, I need to work out how to do things and get error messages, do some research and then get the results I want.
Windows PowerShell is an extensible automation engine from Microsoft, consisting of a command-line shell and associated scripting language (Source: Wikipedia). It now comes standard with Windows Server 2008 R2 and Windows 7. There are some built in command that manage the operating system but there are also modules you can add to add command and script against that product.
To import a module you need to know the module name for example Active Directory is activedirectory and to use the Windows Server Manager Console you need to use servermanager
1: import-module activedirectory
2: import-module servermanager
In Windows 7 you also get Windows PowerShell ISE which stands for Integrated Scripting Environment which is a great way to write scripts and see what is going on with errors. You can run it in this environment to test the script.
As you can see in the middle section of this screenshot when I tried to import the servermanager console returned an error mainly because the module can not be found on this machine.
There are some command line commands that still work in PowerShell, like changing directory is still cd %foldername% or to create a new folder is still md *newnameoffolder*.
There are new commands to do these as well, like change directory is set-location and create a new folder is new-item
To me, I prefer the older version – its quicker and I already know this and when it comes to creating a script I still use the older commands. If you think you are going to have to learn everything in powershell, don’t worry there are some old commands that still work ;-).
The old style batch command were easy to create and they are the same in PowerShell. Instead of using the .bat extension you have a new one called .ps1. Microsoft have really helped with editing the file. If you want to edit the file double click on the file and it will open up in notepad and if you want to run the command you right click on the object and then click Run with PowerShell. The number of times I’ve gone to edit my batch script and I’ve run it by mistake is not worth counting. Now I can’t make this mistake again.
I had to do some research for the next part. I’m in my PowerShell console and I can’t load my .ps1 script. I type in the file name and it just doesn’t run. To run your powershell command type in .file.ps1 and it will run it for you.
I’ll keep you updated on how the learning is going. My objective is to install Active Directory, SQL, Exchange and SharePoint all through PowerShell.