Find path and name of script from script?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 15 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
alec_burgess
Posts: 4
Last visit: Thu Sep 04, 2008 4:18 pm

Find path and name of script from script?

Post by alec_burgess »

Newbie question 2:I want to determine the containing folder and name of the script being executed from within the script. how do I do the equivalent of these lines of VBScript code?==========We are working with two object, the FileSystemObject, and the Shell object. I have created them here.thisDir = objFSO.GetFolder( objFSO.GetParentFolderName( WScript.ScriptFullName ) ) & ""This
line (there should be no line break) retrieves the name of the folder
that you've keeping the backup files in. We could have simply coded in
User avatar
gagaga
Posts: 1
Last visit: Thu Oct 09, 2008 10:16 am

Find path and name of script from script?

Post by gagaga »

To return the script name:
$thisScript = Split-Path $myInvocation.MyCommand.Path -Leaf
To return the parent folder of the script
$scriptRoot = Split-Path (Resolve-Path $myInvocation.MyCommand.Path)
This topic is 15 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked