PowerShell: Display almost anything in a graphical GridView

Out-GridView Dirty Tricks

Out-Gridview is a new cmdlet in PowerShell v.2 which allows you to output objects to a “mini” excel sheet like this:

Get-Process | Out-GridView

However, this only works if .NET Framework 3.51 is installed. While PowerShell requires just .NET 2.0, both Out-GridView and ISE-Editor require 3.51 because of their WPF graphical output.

Out-Gridview will display the same columns you would have seen when you output the objects to the console. You should clone the object first to see all object properties in your gridview:

Get-Process | Select-Object * | Out-Gridview

PowerShell: Sort-Object

You can use Sort-Object to sort simple variable types. Have a look at the following:

‘Tom’, ‘Chris’, ‘Judy’, ‘Alan’ | Sort-Object

Input can come from a different command. If you’d like to get seven random lottery numbers, you should try this:

1..49 | Get-Random -Count 7 | Sort-Object

You’ll find that when you feed complex objects into Sort-Object, you should specify the object property you want to sort on (or else Sort-Object will pick one by itself):

Get-ChildItem $env:windir | Sort-Object Length

Here are some more examples:

Get-HotFix | Sort-Object InstalledOn

Get-ComputerRestorePoint | Sort-Object Description

PowerShell: Creating large dummy files with .NET

Ok, this one’s is cool i think. Whenever i needed a large dummy file, i opened up notepad, write ASDFASDFASDFA CTRL-A, CTRL-C, CTRL-V,CTRL-V,CTRL-V,CTRL-V. CTRL-A, CTRL-C, CTRL-V,CTRL-V,CTRL-V,CTRL-V,CTRL-V.. Then i saved the file, opened up a CMD-Shell and wrote copy dummyFile.txt+dummyFile.txt+dummyFile.txt until the file was large enough.. ok, there’s fsutil.exe which does the same in a much more efficient manner.. OR, unleash the Power of .NET – no, you don’t have to start programming using Visual Studio, just open up PowerShell.. Logo_PowerShell

Continue reading

PowerShell: Running PowerShell Scripts as Scheduled Task

Schtasks.exe – Enables an administrator to create, delete, query, change, run, and end scheduled tasks on a local or remote computer. Running Schtasks.exe without arguments displays the status and next run time for each registered task.

If you have jobs that need to execute regularly, you can manage them with a PowerShell script and make it a scheduled task:


Continue reading

PowerShell: Delete specific files recursively / recursive

Okay, this post I’m going to write down for my personal notes but also for you..
Ever needed to delete a specific file, in my case in need to delete (remove-item) all *.pdb files in one of my Visual Studio Solution.. recursive of course.
To get a list of Files use the Get-ChildItem CmdLet. Extend that command using the –rec or –recurse parameter – so you’ll get all the files recursively, starting from the current path / directory.

Continue reading

PowerShell: Enumerate Files recursive and get full path for each file

To enumerate all files of a specific path recursive and get the full path for each file using Microsoft PowerShell:

Get-ChildItem -rec | ForEach-Object -Process {$_.FullName}

to capture the output and generate an XML file do:

Get-ChildItem -rec | ForEach-Object -Process {$_.FullName} | Export-Clixml c:\temp\report.xml

to only enumerate / list files, use the psIsContainer switch:

Get-ChildItem -rec | Where-object {!$_.psIsContainer -eq $true} | ForEach-Object -Process {$_.FullName}

or for folders only:

Get-ChildItem -rec | Where-object {!$_.psIsContainer -eq $false} | ForEach-Object -Process {$_.FullName}

PowerShell: UAC, SUDO, Elevation, Access to the path ‘C:\Program Files’ is denied.

Powershell on Windows Vista and Windows 7 runs restricted and not-elevated by default. Microsoft did a good job when they invented the so called UAC (User Account Control). As ususal, Microsoft started something new and did not finish it yet – the product is still not feature-complete. Well, probably it’s their philosophy to create thousands of products in a vertical manner, the user then is left all alone finding out how to dive into the features from a horizontal perspective…

What i mean: PowerShell runs restricted and has NO SUDO command. For sake of christ, there is a workaround.

First, find out the location of your profile.ps1. it easy as:

PS C:\> $profile

Probably this script does not exist by default. At least on my systems this was the case. So create it.
When created / if it does exist do:

PS C:\> notepad.exe $profile

This should open your profile.ps1 script in notepad..
Edit this Script to include:

function elevate-process
$file, [string]$arguments = $args;
$psi = new-object System.Diagnostics.ProcessStartInfo $file;
$psi.Arguments = $arguments;
$psi.Verb = "runas";
$psi.WorkingDirectory = get-location;

set-alias sudo elevate-process;

Now you’re almost done. After starting a new PowerShell session – even a non-elevated one – you can run commands elevated by using the sudo command.
For Illustration:

PS C:\> sudo notepad.exe

If the new PowerShell session should display a warning regarding the execution of scripts, check out my older posts (using the site search), i’ve postet a solution for that.