Showing posts with label Graphical Windows PowerShell. Show all posts
Showing posts with label Graphical Windows PowerShell. Show all posts

Tuesday, January 20, 2009

CPU clock cycles

to list all processes running on a local machine that are using more CPU clock cycles than other processes

PS> Get-Process |
ForEach-Object `
{if ($_.cpu -lt 100)
{Write-Host $_.name, $_.cpu -foregroundcolor Yellow}
elseif ($_.cpu -gt 100)
{Write-Host $_.name, $_.cpu -foregroundcolor Cyan}}


We use an if statement to decide on the color of the text to display. If the amount of CPU time is less than 100, then the color of text is yellow. If it is more than 100, then we change the color of the text to cyan.

write-warning = write-verbose = yellow

to display the message

PS> $WarningPreference = "Continue"

Write-Warning "Stopping SQL Service..."

or,

PS> $VerbosePreference = "Continue"

Write-Verbose -message "SQL Service has been started. You may proceed..."

(Yellow on Black if powershell.exe in V1.0 or above)
(Black on White if gpowershell.exe in V2CTP2)
(Black on White if powershell_ise.exe in V2CTP3)

Monday, January 19, 2009

two nested for-loops

to display the progress of two nested for-loops

PS> for ($i = 1; $i -lt 101; $i++ )
{for ($j=0;$j -lt 10000;$j++) {} write-progress -activity "Search in Progress" -status "% Complete:" -percentcomplete $i;}

Explanation: The Write-Progress command includes a status bar heading (-activity), a status line, and the variable, $i (the counter in the for-loop), that indicates the relative completeness of the task. The second for-loop is the time counter.

The characters 'Search in Progress', '% Complete:' and repeating 'o' are shown in yellow on a teal progress bar. Graphical Windows PowerShell (V2CTP2) won't see this animation.