Book Review: Learn Windows PowerShell in a Month of Lunches, Second Edition by Don Jones

During December I decided to dedicate myself to learning Powershell for the next few months. Many of the blogs I follow and some of my respected peers have recommended the Learn Windows PowerShell in a Month of Lunches book and ashamedly I have owned the book and never gotten past the first chapters.

The reason I never did do much with Powershell is because I wasn’t sure it was worth my time and wasn’t sure how I could apply it. However, after seeing the great work of Chrissy LeMaire on DBATOOLS and Rob Sewell at DBA Reports I knew that I could make my life a lot easier. I know from their work that they have done a very large amount of work which I did manually could be automated. Therefore I now had sufficient motivation  to start my Powershell journey.

The book is compromised of the following chapters:
1 Before you begin
2 Meet PowerShell
3 Using the help system
4 Running commands
5 Working with providers
6 The pipeline: connecting commands
7 Adding commands
8 Objects: data by another name
9 The pipeline, deeper
10 Formatting—and why it’s done on the right
11 Filtering and comparisons
12 A practical interlude
13 Remote control: one to one, and one to many
14 Using Windows Management Instrumentation
15 Multitasking with background jobs
16 Working with many objects, one at a time
17 Security alert!
18 Variables: a place to store your stuff
19 Input and output
20 Sessions: remote control with less work
21 You call this scripting?
22 Improving your parameterized script
23 Advanced remoting configuration
24 Using regular expressions to parse text files
25 Additional random tips, tricks, and techniques
26 Using someone else’s script
27 Never the end
28 PowerShell cheat sheet 

Chapter By Chapter Review

1. Before you begin

This is a nice short intro into Powershell and shouldn’t take anyone long to finish.

2 Meet PowerShell

This chapter is mainly about customising the GUI/ISE to whatever your preference is and along with first chapter can probably be done in one lunch.

3. Using the help system

I loved this chapter as the key success within Powershell is becoming familiar and efficient with the Help system and this chapter gives you those skills.

4 Running commands 

Finally we are doing something and in this chapter you are introduced to some simple one liners and how they can be constructed.

5 Working with providers 

In this chapter you learn all about PSproviders.

6 The pipeline: connecting commands 

The pipeline is a critical component of Powershell and is explained in an eloquent way.

7 Adding commands 

Now we start dooing some real stuff with Powershell and I found the Lab at the end particularly enjoyable.

8 Objects: data by another name 

A key concept within Powershell is everything is an object and it is explored in this chapter.

9 The pipeline, deeper 

More awesome Pipeline stuff.

10 Formatting—and why it’s done on the right 

Finally I learned how people were making there reports with Powershell and now I had that knowledge.

11 Filtering and comparisons 

Does what it says on the tin.

12 A practical interlude 

I loved this chapter as it was a series of challenges which helped give me confidence with my Powershell knowledge.

13 Remote control: one to one, and one to many

After this chapter I was a little scared about how easy it would be to hack on a large scale using Powershell.

14 Using Windows Management Instrumentation 

WMI had always scared me and now it doesn’t after using it and its newer CIM implementation.

15 Multitasking with background jobs 

Does what it says on the tin.

16 Working with many objects, one at a time 

Finally we get to the FOREACH command.

17 Security alert!

After this chapter some of my earlier hacking concerns were a little alleviated.

18 Variables: a place to store your stuff

I liked this chapter as I finally feel like we are properly starting to script.

19 Input and output

I wasnt too sure about this chapter as it contained quite basic information  and probably coulkd have been added to another chapter.

20 Sessions: remote control with less work

I understand how sessions work but I am not sure it needed a whole chapter.

21 You call this scripting? & 22 Improving your parameterized script

Finally we are doing some really cool stuff and it is quite surprising how easy it is to create scripts for Auomating Administration tasks.

23 Advanced remoting configuration

I must admit I read the chapter and quickly forgot what was contained in this chapter as I doubt I will need to remember all that detail.

24 Using regular expressions to parse text files

My hatred of regular expressions has now extended to another programming language.

25 Additional random tips, tricks, and techniques

A nice few tidbits of information.

26 Using someone else’s script

A great chapter which is a must read.

27 Never the end

A nice little send off into the big bad world with some Powershell.

28 PowerShell cheat sheet 

A nice little reminder of things which has been covered.

In conclusion this book was a great start to my Powershell journey and I am now working through the Learn PowerShell Toolmaking in a Month of Lunches.

Generate random numbers in PowerShell with Get-Random

Generate random numbers in PowerShell with Get-Random

Have you ever wanted to generate random numbers for passwords, test data…. and wondered how you could easily generate them?

Well I was confronted by the same situation on one of our servers where I needed some random numbers for inputting wait times to test an application but didn’t have my usual tool of choice ( MS Excel ) available to me.

I remembered the Get-Random command from memory but used the Get-Help command to allow me to use the correct syntax.

Executing Get-Random will produce a random number.

get-random
get-random

 

This was quite a good start but then I needed only 4 digit figures and I then had to use the minimum and maximum parameters.

Get-Random -Maximum 9999 -Minimum 1000

2

The above command solved that problem for me and I think it reflects how easily readable the PowerShell language is.

The last scenario I had was producing random numbers for some temporary passwords. I looked at the help for Get-Command and noticed the inputobject and count parameters which I thought would fulfil my need.

Get-Random -InputObject (10000..99999) -Count 3

3

The above command shows the range of values which I gave and the number of random numbers I needed in the Count parameter.

Hopefully the above will help you with your random number needs. Please add your feedback or other solutions in the comments below.

 

List Hotfixes installed with PowerShell with Get-Hotfix

Have you ever wondered what updates have been applied to your Windows operating system?

Well a quick and easy way to do that is to use the PowerShell command Get-Hotfix. This will list out the description, the HotfixID and when it was installed.

I used this on a few PCs where it appeared that updates were not being applied and it showed me when the last updates were installed and hence helped me identify the PCs which were having the trouble and disconnect them from the network.

1

Powershell Alias List

Have you ever wandered whether you could speed up your script writing,shorten down your snippets or come across strange commands in PowerShell?

Well PowerShell’s Aliases would be the answer to all the above scenarios. I first came across them when I saw it in a friends script and could not ascertain what GDR was and is later transpired to be GET-PSDRIVE.

Though I don’t use them in my own scripts as it makes it harder to read, debug and modify. I do believe everyone should familiarise themselves with the alias commands and have listed them below.

Alias Command
% ForEach-Object
? Where-Object
ac Add-Content
asnp Add-PSSnapin
cat Get-Content
cd Set-Location
chdir Set-Location
clc Clear-Content
clear Clear-Host
clhy Clear-History
cli Clear-Item
clp Clear-ItemProperty
cls Clear-Host
clv Clear-Variable
cnsn Connect-PSSession
compare Compare-Object
copy Copy-Item
cp Copy-Item
cpi Copy-Item
cpp Copy-ItemProperty
curl Invoke-WebRequest
cvpa Convert-Path
dbp Disable-PSBreakpoint
del Remove-Item
diff Compare-Object
dir Get-ChildItem
dnsn Disconnect-PSSession
ebp Enable-PSBreakpoint
echo Write-Output
epal Export-Alias
epcsv Export-Csv
epsn Export-PSSession
erase Remove-Item
etsn Enter-PSSession
exsn Exit-PSSession
fc Format-Custom
fl Format-List
foreach ForEach-Object
ft Format-Table
fw Format-Wide
gal Get-Alias
gbp Get-PSBreakpoint
gc Get-Content
gci Get-ChildItem
gcm Get-Command
gcs Get-PSCallStack
gdr Get-PSDrive
ghy Get-History
gi Get-Item
gjb Get-Job
gl Get-Location
gm Get-Member
gmo Get-Module
gp Get-ItemProperty
gps Get-Process
group Group-Object
gsn Get-PSSession
gsnp Get-PSSnapin
gsv Get-Service
gu Get-Unique
gv Get-Variable
gwmi Get-WmiObject
h Get-History
history Get-History
icm Invoke-Command
iex Invoke-Expression
ihy Invoke-History
ii Invoke-Item
ipal Import-Alias
ipcsv Import-Csv
ipmo Import-Module
ipsn Import-PSSession
irm Invoke-RestMethod
ise powershell_ise.exe
iwmi Invoke-WmiMethod
iwr Invoke-WebRequest
kill Stop-Process
lp Out-Printer
ls Get-ChildItem
man help
md mkdir
measure Measure-Object
mi Move-Item
mount New-PSDrive
move Move-Item
mp Move-ItemProperty
mv Move-Item
nal New-Alias
ndr New-PSDrive
ni New-Item
nmo New-Module
npssc New-PSSessionConfigurationFile
nsn New-PSSession
nv New-Variable
ogv Out-GridView
oh Out-Host
popd Pop-Location
ps Get-Process
pushd Push-Location
pwd Get-Location
r Invoke-History
rbp Remove-PSBreakpoint
rcjb Receive-Job
rcsn Receive-PSSession
rd Remove-Item
rdr Remove-PSDrive
ren Rename-Item
ri Remove-Item
rjb Remove-Job
rm Remove-Item
rmdir Remove-Item
rmo Remove-Module
rni Rename-Item
rnp Rename-ItemProperty
rp Remove-ItemProperty
rsn Remove-PSSession
rsnp Remove-PSSnapin
rujb Resume-Job
rv Remove-Variable
rvpa Resolve-Path
rwmi Remove-WmiObject
sajb Start-Job
sal Set-Alias
saps Start-Process
sasv Start-Service
sbp Set-PSBreakpoint
sc Set-Content
select Select-Object
set Set-Variable
shcm Show-Command
si Set-Item
sl Set-Location
sleep Start-Sleep
sls Select-String
sort Sort-Object
sp Set-ItemProperty
spjb Stop-Job
spps Stop-Process
spsv Stop-Service
start Start-Process
sujb Suspend-Job
sv Set-Variable
swmi Set-WmiInstance
tee Tee-Object
trcm Trace-Command
type Get-Content
wget Invoke-WebRequest
where Where-Object
wjb Wait-Job
write Write-Output

 

Output results from PowerShell to CSV or XML

Have you wanted to output results from PowerShell to CSV or XML? Well I am going to show you how.

I have previously explained how to export results of PowerShell queries but did not include how to export the results to CSV or XML. The reason for that is that when you become familiar with PowerShell you will find out that PowerShell commands can return a lot more information than what they show on screen. I will show how to get that information in a future blog post but just be aware that there is a lot more information readily available and only the displayed columns are exported to text(TXT) files.

When exporting to CSV and XML the hidden columns are exported as well. I will give an example of the difference in the exports.

The results of Get-Process are displayed below.

1

If I run the below commands I would output the results of Get-Process to CSV and XML.

Get-Process | Export-CSV c:\PowerShell\ProcessCSV.csv

Get-Process | Export-CliXML c:\PowerShell\ProcessCliXML.xml

2

When you look in the text file you will see the same results as shown on screen.

3

However, the CSV and XML have all the columns available for the results of that command. I have shown an excerpt of the Get-Process results in CSV format below.

4

I would advise that you use TXT, CSV and XML files where appropriate and check out the results you could get from various commands.

Open files using PowerShell Get-Content

Have you ever wanted to open a file and view its contents using PowerShell? Well I am going to show you how.

I frequently output the results of PowerShell queries into files for later review but rarely used PowerShell to view the contents. This has been because I always had other programs readily available to view the contents easily so never thought to use it. The other day I was on a server where we didn’t have access to notepad and I wanted to look at a file I had generated earlier that day.

I used an old DOS command ‘type’ but saw my colleague who frequently worked on that server use a PowerShell command I was unfamiliar with. He was using Get-Content to look at some files so I used the help command to quickly familiarise myself with the command and once again I was smiling at the beauty and ease of PowerShell commands.

Get-Content C:\PowerShell\Process2.txt

1

Simple way to output results to a file using PowerShell

Have you ever wanted to send the output of a PowerShell command to a file? Well I am going to show you how.

When you run a PowerShell command like Get-Process, Get-Host…. It will return useful information but sometimes you want to save that information for future reference. You could cut and paste or take a screenshot but these are way more complicated than simply using the inbuilt functionality within PowerShell.

For example if you wanted to output the processes currently running you would use Get-Process. To output it you would add the below command

Get-Process  >  yourfilename.txt

Get-Process > C:\PowerShell\Process.txt

1

List active process running using PowerShell

Have you ever wanted to quickly find the processes running using Powershell? Wanted an alternate to Task Manager? Well I am going to show you how.

Whenever I get a call from someone about a performance issue on any of our SQL Servers I always usually have a quick glance of Task Manager so that I can help identify what maybe causing the problem. However, the problem can be that on a server which is low on resources it may take a long time to open or the opening of task manager may add to the problems or it may not open at all.

This has led me to frequently opening PowerShell and using it to get the information which I need. The script is quite simple and is below.

Get-Process

1

I have found the advantage of using the script is that it gives me a snapshot whilst in task manager it is always a moving target and if I run it several times I can just scroll up and down to find a pattern which would help diagnose the problem.

Get the Operating System Properties via PowerShell

Have you ever wanted to get all the operating system properties of a computer? Well I am going to show you a short PowerShell script which can get loads of information about the operating system.

One of my colleagues was googling around for information about how to find the windows directory, number of users, last boot up time and many other properties of various computers. I told him that I had a short script which had that information and probably a whole lot more information which he might find useful.

$hostname = “parvinder-pc”

Get-WmiObject -class win32_operatingsystem -computer $hostname | select-object *

1

Search PowerShell help using wildcards

Have you ever wanted to search for new commands or have difficulty remember syntax? Well I recently found the wildcard search function within the help system and I am going to show you how to use it.

You could use the commands feature which is below and can be accessed via Add-ons > Commands

1

Or use the wildcard search in the command line. This is easily done using an asterix in front of, after or around the command or word you’re trying to look for.

For example if I was looking for SQL command iI would use

help *sql*

2

Or if I was looking for all commands which started with ‘remo’ I would use

help remo*

3

I would play about with the wildcard search system and see what cool new things you can find.