# Tuesday, August 14, 2007

I got tired of typing add-pssnapin blah, especially with Quest's AD snap-in's longer than usual name so I came up with a little script that enumerates registered snap-ins, and lists a simple numbered menu showing those that are not already loaded into memory. You just type the number and hit enter to load it. Enter on an empty line exits the script. Yeah, it's brain-dead, but hey, I think I was brain-dead to continually type long commands in every single session to load stuff ;-) 

UPDATE 2007-08-16: I was even more braindead than I realised - I copied up a broken version without the $notloaded array. Ooops. Fixed.

  1. write-progress "Enumerating Snapins" "Registered..."
  2. $snaps = get-pssnapin -r
  3.  
  4. write-progress "Enumerating Snapins" "Loaded..."
  5. $loaded = get-pssnapin | % {$_.name}
  6. $notloaded  = @()
  7.  
  8. write-progress "Enumerating Snapins" "Complete." -Completed # glitchy
  9.  
  10. $i = 0; "";
  11.  
  12. foreach ($snap in $snaps) {
  13.         if (-not($loaded -contains $snap.name)) {
  14.                 Write-Host -fore cyan -nonewline "${i}) "
  15.                 write-host -fore green $snap.name
  16.                     $notloaded  += $snap
  17.                 $i++
  18.         }
  19. }
  20.  
  21. if ($i -eq 0) {
  22.         Write-Warning "Any eligible snapins are already loaded."
  23.         exit;
  24. }
  25.  
  26. $i--;
  27. Write-Host -fore yellow "<enter> to quit"
  28.  
  29. do {
  30.         write-host -nonewline "Load 0 - ${i}: ";
  31.         $choice = [console]::readline()
  32.         if ($choice -and ($choice -lt 0) -or ($choice -gt $i)) {
  33.                 Write-Warning "'${choice}' is out of range!"
  34.                 continue;
  35.         }
  36.         if ($choice) {
  37.                 "loading $($notloaded[$choice].name) ..."
  38.                 add-pssnapin $notloaded[$choice]
  39.         }
  40. } while ($choice)


Just pop it into a function or a ps1 script.

You'll notice how when this is run from a clear screen, the progress bar disappears, then suddenly reappears and the first few menu items are hidden. This is a bug in the powershell.exe host. Vote on it here:

https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=291380&SiteID=99

 

posted on Tuesday, August 14, 2007 4:05:06 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [2] Trackback
Related posts:
PowerShell Script Provider
PowerShell ISE Hacking: Change default save encoding to ASCII
PowerShell 2.0 – PSCX Labs: Invoke-Reflector
PowerShell 2.0 – Developer Essentials #1 – Initializing a Runspace with a Module
SharePoint Resources & Localization – What, Where and Why?
PowerShell 2.0 – Partial Application of Functions and Cmdlets

Referred by:
simple snaps (www.google.com.pk) [Referral]
write (search.live.com) [Referral]
script snap in (www.google.com) [Referral]
simple menu in ps1 script (it.search.yahoo.com) [Referral]
http://thepowershellguy.com/blogs/posh/archive/2007/08/21/po... [Referral]
http://halr9000.com/article/417 [Referral]
time picker script (search.yahoo.com) [Referral]
registered cmdlets not showing up, powershell, CTP3 (www.bing.com) [Referral]
Script to Add Snap In (www.google.com) [Referral]
write (www.bing.com) [Referral]
feed snap script (www.google.com) [Referral]
powershell add snapin sharepoint (www.bing.com) [Referral]
load Picker script (www.google.nl) [Referral]
script snap-in (www.google.nl) [Referral]
script snapin (uk.search.yahoo.com) [Referral]
powershell add snapin (www.bing.com) [Referral]
how to add snapin to ps1 script (www.bing.com) [Referral]
http://halr9000.com/page/27 [Referral]
add-snapin sharepoint 2010 (www.bing.com) [Referral]
http://translate.googleusercontent.com/translate_c?hl=pl&sl=... [Referral]
adding a pssnapin in script (www.bing.com) [Referral]
add snapin sharepoint (www.bing.com) [Referral]
http://technorati.com/faves?sub=addfavbtn&add=http://www.niv... [Referral]
powershell load "add-snapin" (www.bing.com) [Referral]
Monday, August 20, 2007 3:36:38 PM (Eastern Daylight Time, UTC-04:00)
Nice script. You know what, you should have this generate add-pssnapin commands and append to your profile. That'd be a pretty useful addition. Also, why not an "[A]" option to add all of them?

thanks

-hal
Saturday, September 22, 2007 8:38:31 PM (Eastern Daylight Time, UTC-04:00)
Please, find sexy halloween costume here
Comments are closed.