Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In Windows PowerShell, is there a way to "evaluate" a variable so that I have its value pasted into the command prompt? The usage scenario I would like to perform is as follows:

let's say I have a variable $path which contains "C:/Users/Me/Some/Long/Path". I would like to write cd $path into the command prompt and then substitute $path with its value, so that I have cd C:/Users/Me/Some/Long/Path in my command prompt and can manually edit the long path and eventually press enter to change the directory. Is this or a similar behavior available? Would it be possible with just the variable, without the cd at the beginning?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

There is a way - it's a little hacky and won't always work. Taking your cd example, if you type:

cd $path\<TAB>

PowerShell will expand $path to it's value, but only if there are subdirectories. If $path was a leaf directory, PowerShell wouldn't replace the text at all.

Now for the annoying part - PowerShell will do more than expand your variable, it will also include the first possible completion. If that's not what you wanted, you'll have to delete that text. If you know part of the path you want, it's best to add that, e.g.:

cd $path\*part<TAB>

Note I included a leading wildcard - that works great. A trailing wildcard isn't needed, PowerShell assumes you want a trailing wildcard when you hit .

This trick can work with other commands as well, e.g. if you want to complete files, not just directories, try:

cat $path\<TAB>

This trick will work best with paths, though it might work in some other limited circumstances.

This definitely won't work without a command first though - and to be honest, it doesn't matter much what the command name is.

share|improve this answer
    
thanks for the answer! well, I actually wanted to change something in the path above the directory that it originally pointed to, but this sort of does the job, at least its better than typing the whole path manually :) –  matt.kovsky Jan 14 at 16:58
    
I thought of another option. Run: 'echo $path | clip', then 'cd ' and paste from the clipboard however you do that - right click or Alt+Space,E,P, or if you have PSReadLine, Ctrl+V. –  Jason Shirk Jan 14 at 17:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.