In just one hour i will make you a power shell ninja
Upcoming SlideShare
Loading in...5

Like this? Share it with your network


In just one hour i will make you a power shell ninja



A one-hour blast through the basics of PowerShell with particular focus on discovery within the environments, and with a demo at the end of a PowerShell take on Wordpress's Hello Dolly plugin

A one-hour blast through the basics of PowerShell with particular focus on discovery within the environments, and with a demo at the end of a PowerShell take on Wordpress's Hello Dolly plugin



Total Views
Views on SlideShare
Embed Views



3 Embeds 35 31 2 2



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • This is where we use the lyrics you brought along. We want to make something a little akin to Wordpress’s “Hello Dolly” plugin, which shows a random lyric from Hello Dolly on Admin pages <br /> <br /> First, we want to open that text file and load it into a variable. We have a cmdlet that can do that for us called Get-Content <br /> <br /> By default, Get-Content loads a file line-by line into an array of strings. This is perfect for our needs. Try it. Do $lines = Get-Content “filename.txt” <br /> <br /> and then write $lines to the screen. Good. We’re loading the lyrics <br /> <br /> Now we need a cmdlet that can pick a random number for us. Get-Help random should tell you what we have available. <br /> <br /> Now we could use that cmdlet with a -maximum value of the array length, then pick the corresponding element from the array, but we don’t have to, because Get-Random takes pipeline input <br /> <br /> Try it. Try $lines | Get-Random <br /> <br /> Ooooh look. <br /> <br /> Assign that to a variable, $line = ($lines | Get-Random) <br /> <br /> $line <br /> <br /> Now we can package that into a function <br /> <br /> Do ctrl-j, press F and pick Function <br /> <br /> put your code inside <br /> <br /> Function Get-Lyric <br /> { <br /> $lines = Get-Content $homelyrics.txt <br /> $randomline = ($filecontent | Get-Random) <br /> Write-Host $randomline -foregroundcolor yellow <br /> } <br /> <br /> Oooh, you have a function <br /> <br /> Highlight the function. <br /> Press F8 or hit the “run selection” button <br /> Your code is now interpreted and ready to use <br /> Type Get-Lyric in the command area <br /> Press enter <br /> Tada! <br /> <br /> You have a working function. <br /> <br /> But we can make it much more elegant, using the pipeline to shunt data from the left to the right <br /> <br /> Function Get-Lyric <br /> { <br /> Get-Content $homelyrics.txt | Get-Random | Write-Host -foregroundcolor yellow <br /> } <br /> <br /> Now we can get it to run every time we load powershell <br /> <br /> type <br /> <br /> ise $profile.CurrentUserAllHosts <br /> <br /> paste in your function <br /> <br /> Type a function call just underneath it <br /> <br /> Save it. <br /> <br /> Close all your powershell windows <br /> start a new powershell session <br /> <br /> TADA!! <br /> <br /> Now type Get-Lyric <br /> <br /> DOUBLE TADA!! <br /> <br /> And we’re done. <br /> <br /> <br />
  • You have all the information you need now to go ahead and start writing awesome powershell scripts <br /> <br /> You know how to query the help system <br /> <br /> you know how to call commands and use parameters <br /> <br /> you know how to write functions <br /> <br /> You know how to sort, query and filter <br /> <br /> you know about the ISE - and you should REALLY be using the ISE <br /> <br /> you know about the pipeline <br /> <br /> you know how to customise your powershell profile <br /> <br /> But most importantly you know how to find the information you don’t have <br /> <br /> Now, delete the shortcut to cmd.exe and replace it with powershell. Get into the habit of starting powershell instead of a command line and whenever you want to do something, consider if you can do it by typing a command <br /> <br /> Closing Test Questions <br /> <br /> What cmdlets might I use if I want to do something in PowerShell but can’t remember what the exact command is? <br /> <br /> What Cmdlet might I use to fire of a request to a REST Service? <br /> <br /> I want to do a wildcard search of a long array. What cmdlet and what comparison operator might I use? <br /> <br /> What’s the command to open my default profile in the ise? <br /> <br /> I need to know if a folder exists, what cmdlet might I use? <br /> <br /> I want to open a text file, what cmdlet do I use? <br /> <br /> Why is the pipeline important? <br /> <br /> <br /> <br /> <br /> <br />

In just one hour i will make you a power shell ninja Presentation Transcript

  • 1. In Just One Hour I Will Make You A PowerShell Ninja A Flying Start Into PowerShell
  • 2. What you should have In your meeting invite, you should have received a list of pre-reqs ● Laptop running Windows 7 or later (not RT) ● A text file containing, line-by-line, the lyrics to your favourite song ● Your typing fingers ● Optional: exposure to other programming languages such as C, C#, Java, Perl, Python, JavaScript or VBScript ● Distractions are strictly banned. Close all non-powershell windows and put your phone face down on the table.
  • 3. What we will do A crash course into Powershell Basics, including: ● The Raw Language Basics - covered more fully in a later module ● The Help System and exploring the environment ● The Pipeline ● Working with files and folders ● A quick peek at Profiles - covered more fully in a later module It’s a lot to cover in an hour, and we’re bound to get sidetracked, so let’s get started
  • 4. Getting Started This is a hands-on lab. Fire Up PowerShell The icon looks like this:
  • 5. Getting started with Cmdlets Cmdlets are what they sound like. They’re little commands They do stuff, usually fairly simple stuff They Get- stuff. They Set- stuff. They Invoke- stuff. They Test- stuff They’re the tiny engines of powershell Many of them have quick, snappy aliases
  • 6. A thing called the pipeline the pipeline allows you to string commands together, shunting data from left to right
  • 7. OK, Yeah, but it’s better than that Consider the following code snippet
  • 8. The basics summarised ● Variables are prefixed with a dollar sign ● Code blocks are delimited with curly braces ● Assignment operators are =, +, etc ● Comparison Operators are -lt, -gt, -eq, -like etc ● Commands (called Cmdlets in PS World) follow a Verb-Noun convention ● Powershell is extensible via Modules ● But you REALLY don’t need to remember this Because….
  • 9. Powershell is internally-documented There are Cmdlets that tell you about Cmdlets ● Get-Command ● Get-Help ● Get-Alias ● Get-Module ● Get-Member
  • 10. Environment Variables You’ll also want to know about environment variables $Env: $home $psversiontable
  • 11. OK, so what next? So far you’ve just been working at the prompt Now it’s time to start writing basic scripts. First, let’s get set up
  • 12. Simple functions Functions allow you to package up lumps of code and call them from other lumps of code. We’re going to write a function now. Then we’re going to modify it Then we’re going to run it Then (bonus!) we’re going to make it run whenever we start powershell
  • 13. In summary You now have the tools you need to: ● Run commands ● Write scripts ● Find help ● Customise your environment So go forth and be a ninja