magnify
formats

Clipboard Change Directory in PowerShell

Published on July 10, 2012

powershell2xa4Change 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-DirectoryViaClipboard

Download: 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:
Unable to display content. Adobe Flash is required.

thanks to @coridrew for the quick ‘hey you should blog this’ motivation :)

 
Tags:
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 

Leave a Reply

Your email address will not be published. Required fields are marked *

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=""> <strike> <strong>