On my last Friday Fun post on PowerShell prompts, I got a terrific comment from Bart Vandyck about his prompt which has just about everything you would want. I too have a "kitchen sink" prompt, that is to say, one with the proverbial "everything but the kitchen sink". Or you might consider this an extreme prompt.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
This prompt function displays the following information:
- Computer name
- PowerShell session length
- CPU utilization
- Percent free space on C:
- System up time
- Current location
- Amount of free memory
- Number of processes
- A history counter (I liked this idea and "borrowed" it from Bart)
Like Bart, I too like a prompt that allows me to type longer commands before they start wrapping. So I create a green text box with system information. The prompt includes a history counter and a simply PS> moniker. My function also takes advantage of the window title bar. There I display how long the PowerShell session has been running, the computer name and the current location.
Here's the function.
Function prompt { #add global variable if it doesn't already exist if (-Not $global:LastCheck) { $global:LastCheck=Get-Date $global:cdrive=Get-WMIObject -query "Select Freespace,Size from win32_logicaldisk where deviceid='c:'" } #only refresh disk information once every 15 minutes $min=(New-TimeSpan $Global:lastCheck).TotalMinutes if ($min -ge 15) { $global:cdrive=Get-WMIObject -query "Select Freespace,Size from win32_logicaldisk where deviceid='c:'" $global:LastCheck=Get-Date } [int]$freeMem=(Get-Wmiobject -query "Select FreeAndZeroPageListBytes from Win32_PerfFormattedData_PerfOS_Memory").FreeAndZeroPageListBytes/1mb $cpu=(get-wmiobject -class win32_processor).loadpercentage $pcount=(Get-Process).Count $diskinfo="{0:N2}" -f (($global:cdrive.freespace/1gb)/($global:cdrive.size/1gb)*100) #get uptime $time=Get-WmiObject -class Win32_OperatingSystem $t=$time.ConvertToDateTime($time.Lastbootuptime) [TimeSpan]$uptime=New-TimeSpan $t $(get-date) $up="$($uptime.days)d $($uptime.hours)h $($uptime.minutes)m $($uptime.seconds)s" #$text="CPU:"+$cpu+"% Procs:"+$pcount+$diskinfo+ " "+([char]0x25b2)+$up +" "+(Get-Date -format g) $text="CPU:{0}% FreeMem:{6}MB Procs:{1} Free C:{2}% {3}{4} {5}" -f $cpu,$pcount,$diskinfo,([char]0x25b2),$up,(Get-Date -format g),$FreeMem $myPrompt=[char]0x250c $myPrompt+=([char]0x2500).ToString()*$text.length $myPrompt+=[char]0x2510 $myPrompt+="`n" $myPrompt+=([char]0x2502)+$text+([char]0x2502) $myPrompt+="`n" $myPrompt+=[char]0x2514 $myPrompt+=([char]0x2500).ToString()*$text.length $myPrompt+=[char]0x2518 Write-Host $myPrompt -fore Green #get history ID $hid=(Get-History -count 1).id+1 Write-Host "$hid PS>" -nonewline #set the PowerShell session time, computername and current location in the title bar #get start time for the current PowerShell session #$pid is a special variable for the current PowerShell process ID [datetime]$psStart=(get-Process -id $pid).StartTime $ts=(Get-Date) - $psStart #strip off the millisecond part with Substring(). The #millisecond part will come after the last period $s=$ts.ToString() $elapsed=$s.Substring(0,$s.LastIndexOf(".")) $title="[{0}{1}{2}] {3}" -f $elapsed,([char]0x25ba),$env:computername,(Get-Location) $host.ui.rawui.WindowTitle=$title #the function's actual return is nothing return " " }
The function works in both the regular console...
...as well as the ISE.
I expect most of the function should be pretty straightforward. The function uses ASCII characters to draw the box and provide an uptime indicator and separator in the title. I also use the -f formatting operator a lot which is a great way to create strings without having to worry about concatenation.
Because there is some calculating using WMI everytime you hit enter, it takes a few seconds for the prompt to appear. If you use PowerShell interactively and heavily, this may start to get annoying. One thing I've done is to keep a global timer variable. Since some items like free disk space don't change frequently, I only update that value every 15 minutes. But if you are like me with PowerShell window always open, this provides a nice system check that I can refresh simply by pressing enter. I hope you'll let me know what you think.
Download my "everything-but-the-kitchen-sink" prompt here.
Help! I bought your book on PS 2.0 and it has everything but what I purchased it for,
reading amd writing to SQL Server.
1) I want to connect to a sql server and read a list of server names from a table.
2) Use those names to complete a variable used for a connection string
3) For each of the conn strings, connect to the server and execute a simple query.
(select @@version is easy enough)
4) Write to the next column in the initial table we read from, the results of that query.
(final results of the Table after running the script)
server1 Microsoft SQL Server 2008 R2 (RTM) – 10.50.1600.1 (Intel X86)
server2 Microsoft SQL Server 2005 – 9.00.4053.00 (Intel X86)
This would be the model for many things I need to do. After scouring the web and reading the book. I am so lost that I’m totally frustrated. I feel like I’m trying to recreate the wheel.
Can you or someone create this relative simple example to get me off the ground.
Regards,
Tony Garofalo
[email protected]
Thanks for your support. I can get you started in the right direction:
If you have the SQL 2008 snapin, the Invoke-SQLCmd cmdlet should be all you need.
5 PS> Invoke-Sqlcmd -ServerInstance serenity\sqlexpress -Query “Select @@version”
Column1
——-
Microsoft SQL Server 2008 (SP1) – 10.0.2531.0 (X64) …
This is easier than you think. To get more help, post something in the PowerShell forum at ScriptingAnswers.com.
That’s cool Jeffery! The one little thing I also add to the title bar is if 32 bit or 64 bit.