1
ls "d:\" "yasuo500" -recurse -directory 

This command finds the folder 'yasuo500' in d:\media\documents, I want to use 'cd' to get into that directory, but how? i tried this:

$path1 = ls "d:\" "yasuo500" -recurse -directory
cd $path1

But it does not work, it says cd : cannot find path 'c:\yasuo500' because it does not exist.

What am I doing wrong?

*Keep in mind I can just do

cd "d:\media\documents\yasuo500"

But... it's not efficient, what if the directory is LONG, i'd take me too much typing.

  • I am not really clear what you are actually asking. You have the answer you need in your question (or so it seems to me), and the examples/output you give isn't really lining up (your directory is moving from c: to d:). I may be missing something, but I am not sure what anyone can really add to help you, nor how this is a serious issue – iwaseatenbyagrue Mar 4 '17 at 12:10
3

You need: cd $path1.FullName

PowerShell operates on objects, and it's important to understand how these work together.

cd is an alias for Set-Location.
get-help Set-Location indicates it expects a string for the path:

Set-Location [[-Path] < string >] [-PassThru] [-UseTransaction] []

That means when you pass it an object, it will cast the object to a string.

$path1 is a DirectoryInfo, and if you call $path1.ToString() you'll get just the yasuo500 part of the path. It's the equivalent of cd yasuo500 and the error indicates you tried doing that from the root of your C:\.

Using the FullName property of your DirectoryInfo gives a full path string.

  • thanks for the answer, not only does this work, but you explained why my method did not work, thanks! – ScuddleWuddles Mar 4 '17 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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