Formatting
Powershell

It is very good to remember this section to get the formatting right. It will make your life easier when things are well formatted and gives you a good overview.
There are four keys that are important
• Expression: The name of object property to be displayed in this column
• Width: Character width of the column
• Label: Column heading
• Alignment: Right or left justification of the column

So without further due let's format the output of the command dir in a nicer overview

$column1 = @{expression="Name"; width=30;label="filename"; alignment="left"}
$column2 = @{expression="LastWriteTime"; width=40;label="last modification"; alignment="right"}

This will format the first column Name the filename with a width of 30 and left alignment
This will format the second column LastWriteTime with a width of 40 and labelled last modification date alignment right
After these variables are filled you can redirect the output of dir through these formats:
Dir | Format-Table $column1, $column2

The output is a nice formatted list of filename lastwritetime with the label of last modification time
Pretty cool huh?

Let say you like the output of Get-Process but you want to see what is consuming the most cycles and sort on that

Get-Process | Sort-Object CPU -descending

Or if you want to see how many word documents are in a directory:
Dir | Group-Object Extension | Sort-Object Count -descending

After that let's say you have changed the path on the page Usefull Commands you can first find out how many .docs are stored in your directory and then run:
rar a docs.rar *.doc

Or even better first let's find the files that are larger then 1024KB
Dir | Group-Object {$_.Length -gt 1024KB}

When you want to see which letters of the filename are the most common in the directory
Dir | Group-Object {$_.name.SubString(0,1).toUpper()} | Sort-Object count -descending

When you want all the all events out of the eventlog with eventID 1001 and put into a html file
Get-EventLog | Where-Object { $_.EventID -eq "1001" } | ConvertTo-Html | Out-File test.htm

When you want to export let's say a directory into an excel file you can do it like this:
Dir | Export-Csv test1.csv
Get-Content test1.csv | ForEach-Object { $_.replace(',', "`t") }| Out-File test2.csv
./test2.csv

Okay let's break the last command down. We ran the dir command and export it to a csv file called test1.csv.
This is stored in a comma separator file and is hard to read it like this and would be better when it was placed in columns.
So the second command gets the content of file test1.csv and for every object replace the , with a tab and then export it to a new file called test2.csv.
The last line is simple to execute the file and it will open excel or any other program.

If you run the command get-service it will display on default

Status Name DisplayName

What if you want to see other properties?
Well first run the command get-services and pipe it through get-member
Then you will see the properties and now you can run a command like:

Get-Service |where-object {$_.status -eq "running"}| Select-Object CanShutdown ,CanStop ,Displayname |Sort canstop| format-table -auto

This will show all services that are running and list them with CanShutdown CanStop DisplayName

When you want your screen of powershell to look even nicer check out the font consolas:

$key = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont"

## Find out if Consolas is installed on the system
if(!(test-path (join-path $env:windir "Fonts\CONSOLA.TTF")))
{
        write-host "The Consolas font is not installed on the system."
        write-host "Install Microsoft Office 2007 Beta to obtain the font."
        break
}

## Determine if Consolas is already installed as a command window font
$installed = get-itemproperty $key |
             get-member |
             where-object { $_.Name -match "^0+$" } |
             where-object { $_.Definition -match "Consolas" }
if($installed -ne $null)
{
        write-host "The Consolas font is already installed as a command window font."
        break
}

## Find out what the largest string of zeros is
$zeros = (get-itemproperty $key |
         get-member |
         where-object { $_.Name -match "^0+$" } |
         measure-object).Count

## Install the font
new-itemproperty $key -Name ("0" * ($zeros + 1)) -Type string -Value "Consolas"
write-host "Consolas font installed successfully as a command window font."

On Me

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License