Skip to content

Instantly share code, notes, and snippets.

@bender-the-greatest
Created January 5, 2017 05:47
Show Gist options
  • Save bender-the-greatest/a5994e06004bd64aea435ab6083153ea to your computer and use it in GitHub Desktop.
Save bender-the-greatest/a5994e06004bd64aea435ab6083153ea to your computer and use it in GitHub Desktop.
Powershell function which opens the containing folder of a given file
# Powershell function which opens the containing folder of a given file
# in the default program (this will be Windows Explorer 99% of the time).
#
# Note that if -File is actually a folder, it will open that folder's
# parent folder.
#
# Usage:
# Open-ContainingFolder $env:AppData
# Open-ContainingFolder .\somefile.txt
# Open-ContainingFolder C:\Windows\System32\ping.exe
function Open-ContainingFolder {
Param(
[Parameter(Mandatory=$true)]
[string]$File
)
if (Test-Path -LiteralPath $File) {
Invoke-Item $(Split-Path -Parent $File)
} else {
$host.ui.WriteErrorLine("$File does not exist")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment