How to get the directory path of a PS script

Share
Viewing 2 reply threads
  • Author
    Posts
    • #599254
      PowerMe!
      Participant
      Post count: 24
      Member Points: 1,157
      Rank: Level 3

      I am trying to add .PSs file to a PS script :

      . xxxx\test.ps1

      I would like to have the path dynamically created based on the location of the main script file.

      I tried $script:MyInvocation.MyCommand.Path

      $PSScriptRoot

      $PSCommandPath

      none of them work.

      I am using Pshell ISE and version 5.1.16299.492

       

      Thanks

      Ratan.

       

      1+
    • #599653
      David Figueroa
      Participant
      Post count: 12
      Member Points: 2,622
      Rank: Level 3

      You should be able to use

      Split-Path -path ($MyInvocation.InvocationName) -Parent)

      (I give up trying to use the preformatted section, or the code display.. neither of them seems to be working correctly for me).

      David F.

      1+
      • #600066
        Michael Pietroforte
        Keymaster
        Post count: 1893
        Member Points: 24,844
        Author of the year 2018
        Rank: Level 4

        You mean the pre and the code tags here in the forum? What was the problem?

        1+
        • #605736
          David Figueroa
          Participant
          Post count: 12
          Member Points: 2,622
          Rank: Level 3

          It shows up incorrectly.. here are two examples..  both here and I’ll attach screenshots.
          <pre class=”crayon-selected”>Write-Verbose -Message ‘This is using the [] block tags (pre)’

          <pre class=”lang:ps decode:true “>Write-Verbose -Message ‘This is using the powershell code tag’

          David F.

          1+
        • #605741
          David Figueroa
          Participant
          Post count: 12
          Member Points: 2,622
          Rank: Level 3

          And now I may be having trouble attaching screenshots.. I’m trying again (using Chrome),

          1+
        • #605825
          Michael Pietroforte
          Keymaster
          Post count: 1893
          Member Points: 24,844
          Author of the year 2018
          Rank: Level 4

          Did you close the pre tags? You can add screenshots through the toolbar in the Visual editor.

          1+
        • #607710
          David Figueroa
          Participant
          Post count: 12
          Member Points: 2,622
          Rank: Level 3

          I’ve tried it without the tags and I added the missing tags, and got the same results each time.  I had not tried the toolbar.. I had been trying to use the attach file at the bottom.

           

          While editing...

          I did notice when I tried the block tags, the closing pre tag was present
          <pre class=””>Write-verbose -Message ‘Sample’
          And here is my sample using the code view
          <pre class=”lang:ps decode:true “>Write-verbose -Message ‘Code view sample’
          And the relevant screenshots:
          Shows the closing pre tag is there..

          Showing the visual view of the statement with the closing pre tag

          Inserting code (text view)
          (I had used the button, I didn’t try to “free hand” it)

          Inserting code from the visual side

          Hopefully that helps..

          David F.

          1+
        • #607794
          Michael Pietroforte
          Keymaster
          Post count: 1893
          Member Points: 24,844
          Author of the year 2018
          Rank: Level 4

          It seems you tried to add the HTML tags in the Visual editor. You have to click the Pre icon in the tool bar here. Best way in the Visual Editor is to write the complete post and then select the parts that you want to mark as code. It works:

          In text editor (click the Text tab), you can enter the Pre tags manually.

          If you still have problems, please post something in the Community forum.

          1+
        • #607829
          David Figueroa
          Participant
          Post count: 12
          Member Points: 2,622
          Rank: Level 3

          That’s exactly what I did..  but I’ll move over to the community forum and stop hijacking this thread 🙂

           

          David F

          1+
        • #621681
          Michael Pietroforte
          Keymaster
          Post count: 1893
          Member Points: 24,844
          Author of the year 2018
          Rank: Level 4

          It seems in a recent bbpress update some HTML tags were disabled for participants. I now added the pre and the code tags to the allowed tags.

          Thanks for reporting and sorry for the trouble.

          2+

          Users who have liked this topic:

          • avatar
    • #599810
      PowerMe!
      Participant
      Post count: 24
      Member Points: 1,157
      Rank: Level 3

      Thank you David. Sure it did work.

      I also realized that I was trying to use my script file that was on the OneDrive folder on my computer! That’s why the file path was returning and empty string. So I did a test on the local drive. I thought the following would of interest.

      1+
Viewing 2 reply threads
  • You must be logged in to reply to this topic.
© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account