2

I have this:

 Get-ChildItem c:\H\admin\contents

Is it possible to do the same thing but relative to the folder that the powershell script runs in. Sometimes I have the script running in c\H\admin and sometimes in the c\H\user directory so I would prefer not to have to change all the paths in my scripts.

  • Similar post as well: http://stackoverflow.com/questions/5466329/whats-the-best-way-to-determine-the-location-of-the-current-powershell-script – Matt Apr 09 '16 at 03:02

1 Answers1

4

Yes, you can use relative paths.

Get-ChildItem .

refers to the current directory, c:\H\admin\contents.

Get-ChildItem ..

refers to the parent directory, c:\H\admin

Get-ChildItem ..\..

refers to the parent of the parent, c:\H

You can use relative paths related to a specific path:

Get-ChildItem c:\H\admin\..\users

which would refer to the folder c:\H\users

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 2
    There has to be a dupe for this. – Matt Apr 09 '16 at 02:52
  • 3
    @Matt: Apparently you didn't find one, or you would have voted to close the question instead of commenting on an answer. :-) Thanks for your support. – Ken White Apr 09 '16 at 02:55
  • 2
    Not so fast.... I am looking for one. – Matt Apr 09 '16 at 02:56
  • 3
    Great. Thanks for the information. Can I look at every answer you've ever posted that might have a dupe as well, and inform you every step of the way? Or, better yet: Can I ask why you're telling *me* that the poster may have posted a duplicate, rather than telling the poster? I didn't post the question. – Ken White Apr 09 '16 at 02:57
  • 1
    I'm sure you are aware there is nothing wrong with dupes... I'm sure there are a bunch of mine somewhere especially the earlier ones. It doesn't make the answers wrong? Body language is lost in text but I didn't think I was saying anything wrong... Sorry about that. – Matt Apr 09 '16 at 03:00