Creating a PowerShell Module to Restart Explorer.exe
I've been doing a fair bit of development lately with Shell Extensions and have to constantly restart my explorer.exe process. Quickly, I found that I could do this through powershell using the Stop-Process
commandlet.
Stop-Process -ProcessName explorer
This was easy enough to type, but I found myself doing it quite a bit and wanted it to be quicker and easier to remember. I had wanted to learn more about PowerShell modules, so I figure this is as good of a time as any to try it out.
By default PowerShell will look in your %AppProfile%\Documents\WindowsPowerShell\Modules
folder for any modules that should be loaded when you start a new shell. (If you want to see all the places it looks, you can look at the $env:PSModulePath
variable) From there, each module will have its own folder. So I created a RestartExplorer
folder and created a RestartExplorer.psm1
file which contains my module code:
function Restart-Explorer()
{
Stop-Process -ProcessName explorer
}
Set-alias re Restart-Explorer
Export-ModuleMember -Function Restart-Explorer -Alias re
Now when I type re
, explorer restarts for me. A few things to note in this script. The Export-ModuleMember
commandlet tells PowerShell which components from this script should be available to the shell. If I did not include the -Alias re
then re
would not work from the shell window. If you have a lot of functions and aliases you want to export from a module, you could simply export them all using wildcards:
Export-ModuleMember -Function * -Alias *
While this is a simple example, it shows how easy it is to write modules in PowerShell and have them "just work" once you set them up.