PowerShell: Hello World

Alright, it’s time I started blogging about PowerShell. In fact it’s so long overdue that I really don’t know were to get started. There are so many great resources and blogs available now that most people who are motivated to use PowerShell have already gotten off the starting block.

I suppose the best way to get started with PowerShell is to just open the console and start using the DOS or UNIX commands that you may already be used to.

Right, open the PowerShell console and start typing these familiar commands: DIR, CD and MD.

image

As you can see, they all still work. As do CLS, MKDIR, COPY etc. That’s because they are in fact PowerShell Alias’s. This means that they’re just PowerShell commands with familiar names. Here’s a few common ones.

DIR Get-ChildItem
CLS Clear-Host
MKDIR, MD New-Item –ItemType Directory
CD Set-Location
COPY Copy-Item
ECHO Write-Output

Steve Tibbett has created an excellent cheatsheet here. What’s cool is that you can create your own aliases as you go along.

PowerShell commands also have the ability to be run in a script with the .ps1 extension but running scripts are disabled by default for security purposes. Firstly, you will need to set the Execution Policy. The cmdlet Get-ExecutionPolicy will show you that the initial policy is set to ‘Restricted’. This will prevent scripts from running. You can change it with the Set-ExecutionPolicy cmdlet. There are four options Restricted, Remote-signed, Signed and Unrestricted. I recommend Remote-Signed as it will allow you to run the scripts that you have created locally and still keep your machine secure. So, just type

Set-ExecutionPolicy RemoteSigned

Now you can use notepad to create your first script. In Notepad type:

Write-Host "Hello World"

This will need to be saved in a file called something like MyScript.ps1.

Then from a PowerShell console type

.\MyScript.ps1

The reason you need the .\ is because a PowerShell security feature ensures that you are targeting a specific script in a specific location,. And that’s all there is to it.

Congratulations, you’re now a PowerShell scripter! Now, all you need to do is build up your vocabulary.

Advertisements

About Andrew Barnes

A Scripting and Deployment Specialist.
This entry was posted in PowerShell, Scripting and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s