PowerShell for Windows Admins

Feb 17 2014   2:34AM GMT

PowerShell Hello World

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I had a comment left on one of my posts recently that mentioned a “Hello World” script. The concept of a Hello World program/script is that it is a simple introduction to using a new language that gives a known result.

For a brand new user to PowerShell is is how you create a Hello World script:

open PowerShell with elevated privileges (Run as Administrator)

type  Set-ExecutionPolicy RemoteSigned –Force and press enter

This enables scripts to run.

Run these two lines by typing them individually and pressing enter

New-Item -Path c:\ -Name TestScripts -ItemType Directory cd C:\TestScripts

They create a test folder and move your location to that folder

Type

“‘Hello World’” > script1.ps1

and press enter.  This creates your script. The single quotes nested in the double quotes are so that the text in the script will be surrounded by quotes – its a string value.

Run the script by typing

.\script1.ps1

and pressing enter.  You will see the results:

Hello World

The .\ is required as PowerShell won’t run commands in the current folder by default. You have to explicitly give the local path.

You’ve now written your first PowerShell script and discovered most of the “gotchas” that cause people problems when they are first learning PowerShell

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: