invoke item and initializing code

Invoke-Item

invoke-item is a really useful cmdlet that's similar to the open command in OS X. Anything that would normally be typed in the Run window can be passed to invoke-item with the same result. By using invoke-item in PowerShell, though, you get the added benefit of a scripting environment -- which means you can add variables and logic to the parameter.

invoke-item c:\windows\system32\notepad.exe
invoke-item "c:\Program Files"
invoke-item $(split-path $Profile -parent)
invoke-item \\fileserver\Shared

Since I'm familiar with typing open from using a Mac, I made an alias for invoke-item called open:

new-item -path alias:open -value invoke-item

Initializing Code

The only downside to the alias is that I have to recreate it each time I open a new PowerShell session. To solve this, I simply added the new-item command above to a local file called aliases.ps1 and edited my $Profile file:

. c:\aliases.ps1

Realizing my Profile file will start to accumulate a lot of entries like this, I decided to abstract and automate the initialization. I created a folder in my profile directory (located at $Profile) called Includes. I copied my aliases.ps1 file and the itunes.ps1 file (described in my Alarm Clock article) there. Finally, I added the following to my Profile file:

foreach ($f in get-childitem $(join-path $(split-path $profile -Parent) "Includes")) {
    . $f.fullname
}

Now anything inside the Includes directory will automatically get initialized at the beginning of each PowerShell session. This reminds me a lot like the conf.d directories found in *nix environments.

Of course the glaring security hole is that if the PowerShell Execution Policy is set to "unrestricted" and someone has access to that directory, they can simply add any file they like and it'll automatically be run.