Get script to reference its own directory

Welcome Forums General PowerShell Q&A Get script to reference its own directory

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
4 years, 6 months ago.

  • Author
    Posts
  • #16698

    Participant
    Points: 1
    Rank: Member

    Hi All,

    I've got a script that I've written that does some Excel automation. It takes three spreadsheets, does a variety of vlookups/formatting etc and then saves all the data in one sheet. It's working fine.

    However, the file paths for the three reference spreadsheets within the script are currently static – i.e.:

    C:\users\thickgit\Desktop\PSScript\sheet1.xlsx,
    C:\users\thickgit\Desktop\PSScript\sheet2.xlsx,
    C:\users\thickgit\Desktop\PSScript\sheet1.xlsx etc

    I want to give my script to a friend, have him create a folder in any location, put the script and his three spreadsheets in the same folder and just run the script so that he ends up with the final spreadsheet in the same folder.

    How do you essentially put a '.\sheet1.xls' file path into your script so it will locate the files that are in the same directory as the script (and also output the final data to that folder too?)

    Cheers.

    TG

  • #16699

    Participant
    Points: 0
    Rank: Member

    The path from which the script is being run is stored in the automatic variable $PSScriptRoot

    For more information about automatic variables checkout the help document about_Automatic_Variables.

  • #16701

    Participant
    Points: 1
    Rank: Member

    Thanks Simon. I tried something similar earlier, but had a problem with it because I was using single quotes instead of double quotes. It's working now. Cheers.

The topic ‘Get script to reference its own directory’ is closed to new replies.