0

So, I can find out the directory of the script with

function Get-ScriptDirectory { Split-Path -parent $PSCommandPath }

But I can't manage to change the directory to the folder, where the script is saved. For example

cd d:\MyScripts\

So something like

cd function Get-ScriptDirectory { Split-Path -parent $PSCommandPath }

doesn't work.

And sorry if that is a common and dumb question, but I'm new in powershell and I searched and tried a lot before posting here

1 Answer 1

2

You can do it like below. You can include the code sample in your script. Then if you are running the script from other location (by means of fully qualifying path name to the script); $MyInvocation will pickup the invocation location.

From Documentation

$MyInvocation
   Contains an information about the current command, such as the name, 
   parameters, parameter values, and information about how the command was
   started, called, or "invoked," such as the name of the script that called
   the current command.

Code Sample

$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
cd $dir
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.