Change directories much in PowerShell? Here’s a handy quick command to change directory to what’s in the clipboard. I use a command “ccd” for Clipboard Change Directory to change directory to what’s in the clipboard. Usually I’ll copy a file or directory path from Windows Explorer, then just ccd to change to its directory.
function Get-Clipboard {
$command = {
add-type -an system.windows.forms
[System.Windows.Forms.Clipboard]::GetText()
}
powershell -sta -noprofile -command $command
}
function Set-DirectoryViaClipboard {
$dir = get-clipboard
$dir = $dir.Trim("`"")
if ([System.IO.Directory]::Exists($dir)) { cd $dir }
else {
$dir = [System.IO.Path]::GetDirectoryName($dir)
if ([System.IO.Directory]::Exists($dir)) { cd $dir }
}
}
set-alias ccd Set-DirectoryViaClipboardDownload: http://coad.net/blog/resources/clipboardPSScript.7z
To get this into your normal shell environment, edit your startup $profile file: notepad $profile then either (1) put those lines above in the file or (2) put that into a .ps1 file, say clipboard.ps1, and add the line . [filepath] where [filepath] is the location of the clipboard.ps1 file. Then restart your shell.
p.s. It’s also nice to have the PowerShell environment open in the directory in the clipboard. Just put “ccd” at the end of your $profile startup file.
See it in action and how to configure via this video:
thanks to @coridrew for the quick ‘hey you should blog this’ motivation







