DCSIMG
Show or hide your desktop icons with PowerShell - Shay Levy

Shay Levy

If you repeat it, PowerShell it!

News


btn_donate_LG

View Shay Levy's profile on LinkedIn Follow Shay Levy at Twitter Shay Levy's Facebook profile Subscribe to my FriendFeed


site statistics




Show or hide your desktop icons with PowerShell

I was reading through James' blog post and remembered that I wrote a script some time ago to show or hide desktop icons. The script requires PowerShell 2.0 and shows how you can access WIN32 APIs.


#requires -Version 2.0

$signature = @"
[DllImport("user32.dll")] 
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 
[DllImport("user32.dll")] 
public static extern bool ShowWindow(IntPtr hWnd,int nCmdShow);
"@

$icons = Add-Type -MemberDefinition $signature -Name Win32Window `
         -Namespace ScriptFanatic.WinAPI -passThru

$hWnd=$icons::FindWindow("Progman","Program Manager")

function Hide-DesktopIcons{$null = $icons::ShowWindow($hWnd,0) }
function Show-DesktopIcons{$null = $icons::ShowWindow($hWnd,5) }



How to use it

Save the code in script file, say 'Set-DesktopIcons.ps1' and load it via Import-Module cmdlet (i.e Import-Module .\Set-DesktopIcons.ps1). By default, Import-Module export all module members (functions/aliases/variables etc) defined in the module into your session. To specify which members to export use the Export-ModuleMember cmdlet. Now that you've loaded the module you can call the exported functions just like any other cmdlet. PowerShell will auto complete the functions name as well; just type: 'Hide-' or ‘Show-' and press the TAB key.

There is a great post by Lee holmes to guide you through the process of invoking WIN32 APIs in PowerShell. It helped me a lot with the script.

Comments

Twitted by ShayLevy said:

Pingback from  Twitted by ShayLevy

# May 11, 2009 1:23 PM

Andy said:

Thanx  again Shay !

# May 12, 2009 1:39 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: