Read out the current directory of the * .ps1 file

Welcome Forums General PowerShell Q&A Read out the current directory of the * .ps1 file

  • This topic has 3 replies, 3 voices, and was last updated 3 weeks ago by
    Participant
    .
Viewing 3 reply threads
  • Author
    Posts
    • #236119
      Participant
      Topics: 5
      Replies: 9
      Points: 74
      Rank: Member

      Hello everybody.
      I have a PowerShell file x.ps1. This is called up by a link.
      With (Get-Location) .Path I read my current directory. This is output differently when I run the code directly in the ps1 file than when I call the ps1 file through the link.

      Give it another command so I can get my current directory. That of the ps1 file. No matter how I call it. Or is there a way in the ps1 file to determine by code whether this file is executed itself or is started by a shortcut?

    • #236173
      Participant
      Topics: 24
      Replies: 175
      Points: 587
      Helping Hand
      Rank: Major Contributor

      here is how i retrieve the location that the running ps1 is called from.  I use this quite heavily

      $path         = Split-Path -parent $MyInvocation.MyCommand.Definition
      
      
    • #236176
      Participant
      Topics: 2
      Replies: 3
      Points: 34
      Rank: Member

      Try the built-in variable  $PSScriptRoot

      that should get you the directory of the .ps1 file.
      • This reply was modified 3 weeks ago by mraol_posh.
    • #236440
      Participant
      Topics: 5
      Replies: 9
      Points: 74
      Rank: Member
      @David Schmidtberger;@mraol_posh,
      Hello,
      both solutions works.
      Many thanks for your help.
Viewing 3 reply threads
  • You must be logged in to reply to this topic.