POWERSHELL: Get Path of Currently Executing Script

A Tech Lead by profession, a Data Enthusiast and a forever learner. Experienced in DevOps, Cloud, FullStack Development, Data Science, Machine Learning and AI.
Search for a command to run...

A Tech Lead by profession, a Data Enthusiast and a forever learner. Experienced in DevOps, Cloud, FullStack Development, Data Science, Machine Learning and AI.
No comments yet. Be the first to comment.
Hashnode introduced 'headless' and while having a look at it, It really seemed interesting. It's nice to have a way to play with our publications and posts from Hashnode, outside of Hashnode without tedious data scraping. While reading another blog, ...

While trying to get Red Hat (RHEL9) installed in my WSL (Windows Subsystem for Linux) within my Windows 11 laptop, I stumbled upon the same hurdle I did for installing the older RHEL8. The list of available distros published by Microsoft still does n...

When it comes to collaborative software development, efficiency in communication and planning are critical. GitHub offers a feature called "Code owners" to improve project management and facilitate cooperation. Let's see how this can improve your dev...

PowerShell is a powerful tool, allowing to perform a myriad of tasks with just a few commands. However, there are times when elevated privileges are required to execute certain commands. This is as easy as right-clicking PowerShell and "Run As Admini...

While trying to setup RHEL with WSL 2, I stumbled upon this very helpful blog https://wsl.dev/mobyrhel8/ While doing the setup, I found this zsh theme very nice, and I have been using it since then. The above mentioned blog already has the steps, but...

Another interesting scripting day for Powershell Kid and suddenly he is stuck at a simple problem. He can’t keep mentioning the full directory path every time explicitly in the script. Because, next minute if he moves the script to some other location, the script fails!! Let’s look into the problem he is facing.
Powershell Kid creates an awesome script that uses a few other files for input and output. So his script is saved at the location
D:\My Test Scripts\The Crazy Script\CrazyPS.ps1
and the script uses external files for input and output purposes, something like
$inputcontent = Get-Content "D:\My Test Scripts\The Crazy Script\bin\input.txt"
....
....
....
$output >> "D:\My Test Scripts\The Crazy Script\bin\output.txt"
Now the script executes marvelously, and the Powershell Kid goes the EUREKA!! mode.
Now he moves the script folder “The Crazy Script” to his favorite folder
"C:\My Fav Scripts\The Crazy Script"
and executes it again from the new location. Disaster! It throws errors like flying dishes. What should he do now?
Hey Powershell Kid, don’t use full directory paths, rather use relative paths. At the beginning of the script mention the following line:
$ScriptPath = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
Now you can refer this variable as the relative path, like:
$ScriptPath = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
$inputcontent = Get-Content "$ScriptPath\bin\input.txt"
....
....
....
....
$output >> "$ScriptPath\bin\output.txt"
Have a fun day scripting!!