Show or hide your desktop icons with PowerShell

May 11, 2009

one comment

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 = @”
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 
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.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


one comment

  1. AndyMay 12, 2009 ב 01:39

    Thanx again Shay !