Windows PowerShell for Newbies

PowerShell is Microsoft’s next generation command-line shell that helps IT guys all over the world to automate administrative tasks.

Getting started with PowerShell

If you have been around the IT business as long as I have you have properly made a lot for .bat files and batch programming to simplify and automate IT tasks. You properly also know that in the year 2011 it is quiet limited what you can do with batch programming in Windows operating systems. It is a very limited set of tools we have. You can’t even access the registry. With PowerShell Microsoft has made a brand new shell that gives us all the possibility we could wish for (Registry access, WMI, Windows processes, Services etc.). In this little intro to PowerShell I will show you what kind of thinks you easily can do with PowerShell even if you have not worked with it before.

To start up PowerShell in Windows 7 go to the Start menu -> Search for programs and type in Powershell.

Powershell Icon
Click on the Windows PowerShell icon and this little command shell window pops up.

PowerShell console

Let us start with some simple commands:

PS C:\>Get-Command
This command shows a list of available cmdlet’s (pronounced “command-let”). This is kind of the tools we got to work with and they can be combined in so many ways that only imagination is the limit.

PS C:\>New-Item C:\Scripts\ -type directory
This command creates a Directory named Scripts in the root of the C:\ drive if it does not already exists.

PS C:\>get-wmiobject win32_bios
Get BIOS information on the machine where the command is executed via WMI.

How to execute a PS1 script

Instead of using the old .bat or .cmd extensions PowerShell has its own extension named .ps1. As per default we are not allowed to execute Powershell scripts in Windows 7, because it is disabled for security reasons I guess. A very powerful script shell is also a very powerful virus shell. Keep that in mind. If you try to execute a .ps1 script you will get this error:

File C:\Scripts\CreateDir.ps1 cannot be loaded because the execution of scripts is disabled on this system.

If you execute the following command:  Get-ExecutionPolicy

You will get this result: Restricted

To enable script executions do the following:

  1. Execute the command line: Set-ExecutionPolicy unrestricted
  2. Enter Y to accept the change of Execution policy
  3. Execute the command line: Get-ExecutionPolicy and make sure that it now returns Unrestricted

PowerShell script Execution policy

Now lest take a look on a little script that I have made named CheckFolder.ps1

$Foldername = “Scripts”
clear
c:
cd\
if(test-path $Foldername -pathtype container)
{
echo “Folder: $Foldername Exists”
}
else
{
echo
“Folder: $Foldername does not”

}

What this little script does it that it checks if the folder Scripts exists in the root of my C:\ drive and returns the result. To execute this script from the shell, now that we have the permission to do it, I just enter:  .\CheckFolder.ps1 in the powershell console

The “.\” in front of the script name tells PowerShell that it is to be executed from the current directory. I could also have entered the full path to the script as well: C:\Scripts\CheckFolder.ps1 Powershell will not just accept me to type CheckFolder.ps1 you always need to enter the path as well

If you want to be a successful Microsoft System Administrator you better learn Widows Powershell. Both Windows Server, SQL Server, Exchange Server and the System Center products are all strongly integrated with Powershell.  For more information about PowerShell check out Wiki and Microsoft Technet

Now let me hear about you and PowerShell:

What do you think about PowerShell?
Have you been working with PowerShell?

Related posts

2 Thoughts to “Windows PowerShell for Newbies”

  1. Adrienne

    I’m so glad we have people like you to explain this stuff. Man, this sounds so confusing to me but I’m no tech gal and I also don’t have Windows 7. Still have XP and loving it. But you definitely explain things in great detail so I have no doubt this is going to come in real handy for others interested in this. So for that, I applaud you Thomas.

    Now, enjoy your day!

    Adrienne

    1. Thomas

      Thank you Adrienne
      I must admit that Powershell was quiet confusing to me in the beginning as well. I have spent hours on other sites to find examples to figure out how it works.
      Thank you for taking you the time to comment Adrienne.

Leave a Comment