Powershell and me

It’s an interesting scripting language. Well, scripting has been for a long time in *nix systems, and I feel it’s quite late that Windows catch up now. But still it’s better than nothing, and I appreciate Microsoft for it. MS seems to be losing it’s power to be blamed for it’s evil or laziness in this post-pc era, anyway.

Learning Powershell

The best way to learn something is 1) to use it everyday (like a young boy practices kung fu while wiping his master’s car window in “Best kid”, I believe. In that sense, I’m lucky that I uses it everyday at Huddle (at the moment, as of 2012).

Another good ways, not as good as the first one, are to read good books about the topic and to answer others’ questions on Q & A sites like StackOverflow. Probably, using it everyday and reading a book about it would be a fantastic combination.

I’m reading Windows Powershell for Developers by Douglas Finke. Very thin, but interesting, informative, and easy read with causual 1:1 chat style.

useful resources

Installing Powershell

It comes preinstalled with Windows 7, Windows Server 2008 R2, Windows 8, and Windows Server 2012 (if you have)
If you want try PowerShell v3, you can download it at http://www.microsoft.com/en-us/download/details.aspx?id=29939

PowerShell has execution policy. By default, it can’t load the profile and others.
By running get-executionpolicy, you can check the policy on your machine.
The recommended setting to begin with is “remotesigned”.

PS D:\dev> get-executionpolicy
RemoteSigned
PS D:\dev>set-executionpolicy remotesigned

Determining the version of PowerShell installed

$PSVersionTable will be the usual answer, as you do like this.

PS D:\dev> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      2.0
PSCompatibleVersions           {1.0, 2.0}
BuildVersion                   6.1.7601.17514
PSRemotingProtocolVersion      2.1
WSManStackVersion              2.0
CLRVersion                     4.0.30319.269
SerializationVersion           1.1.0.1

But $PSVertionTable was introduced with PowerShell version 2. so, if it doesn’t work, you need to do $Host.Version

PS D:\dev> $Host.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1

simple scripts with files, process …

the examples below are from Jeff Alexander’s screencast.

# showing the list of files
Get-Childitem c:\

# format the output with pipe |
dir c:\windows\explorer.exe | Format-List *

# Choose the columns that you want
dir c:\ | Format-Table Name, Extension, CreationTime

# Lists only first 10 lines
Get-Childitem c:\windows\system32\*.dll | select-object -first 10

# Grouping by file extension
Get-Childitem c:\windows\system32 | Group-Object extension

# Lists files updated in 10 days, leveraging DateTime
Get-Childitem c:\windows\system32 | Where-Object {$_.LastWriteTime -gt $($(Get-Date).adddays(-10))}

# Sort process by process id
Get-Process | Sort-Object ID

# WMI to access system information
Get-WmiObject win32_operatingsystem

# Access registries
Get-Childitem hkcu:\Software\Microsoft

# Stop a process
Get-Process notepad | stop-process

Pipeline

from PowerShell Essentials for the Busy Admin (Part 2)

It allows ability to easily work at command line

  • to retrieve items and work on them
  • to filter out data
  • to persist information
  • and to format output
# Start Process
"notepad", "calc" | foreach { start-process $_ }

# Filter processes using too much CPU or Memory
Get-Process | where { $_.pm -gt 20MB }
Get-Process | where { $_.cpu -gt 10 }

# Sort processes
Get-Process | sort cpu -Descending

# Sort event log entries
Get-EventLog -LogName application -EntryType error | sort source | group source

# Pipe to present
get-process | format-table name, id -AutoSize
Get-Service | where {$_.status -eq "running" } | Format-List *

# Pipe to persist information
get-process | format-table name, id -AutoSize | out-file c:\temp\processtable.txt
gps | select name, id | Export-Csv -Path c:\temp\processCsv.csv

Handy commands

# Measure-command measures the performance of your command.
Measure-command {Get-EventLog -LogName application -EntryType error | sort source | group source}

# h lists all of your commands in the console
PS D:\dev\> h

  Id CommandLine
  -- -----------
  11 Get-Childitem c:\
  12 get-childitem c:\
  13 get-childitem c:\ | Format-List *
  14 dir c:\windows\explorer.exe | Format-List *
  15 dir \ | Format-Table Name, Extension, CreationTime
  16 dir c:\ | Format-Table Name, Extension, CreationTime
  17 Get-Childitem c:\windows\system32\*.dll | select-object -first 10
  18 Get-Childitem c:\windows\system32 | Group-Object extension

Powershell and me

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