Monday, 28 November 2011

PowerShell functions, scripts and aliases

Monday 28/11/2011 – 8:00 PM    


 

While I have been using PowerShell for years now, I have only used it as a literal replacement for the command prompt without benefiting from the additional functionality that PowerShell brings. However, in the past year or so, I have been come to see the light and have been using PowerShell for doing things that are either difficult or impossible to do in the command prompt.


 

Here's a list of functions, scripts and aliases that I use on frequent or semi-frequent basis. I am very interested in knowing if they can benefit someone else. I also welcome suggestions on improving and expanding them.


 

S#

Item

Type

Commands

Description

Examples

1

mf

Function

function mf($path)

{

ni –type file $path

}

Make a file

mf d:\a.txt

2

uptime

Function

function uptime()

{

[string]::Format( "{0:0} Hours", [System.Environment]::TickCount/3600000)

}

Get number of hours since machine was last restarted

uptime

3

fs

Function

function fs()

{

    $disks = Get-WmiObject -ComputerName "." -Class Win32_LogicalDisk

    

    foreach ($disk in $disks)

    {

        if ($disk.DriveType -eq 3)

        {

            [string]::Format("Drive Letter: {0:N0}", $disk.DeviceID)

            [string]::Format("Volume Name: {0:N0}", $disk.VolumeName)

            [string]::Format("Total Size: {0:N0} MB", $disk.Size/(1024*1024))

            [string]::Format("Free Space: {0:N0} MB", $disk.FreeSpace/(1024*1024))

            echo ""

        }

    }

}

Display the Drive Letter, Volume Name, Total Size and Free Space of all hard drive partitions

fs