Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4029508
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:21:15+00:00 2026-05-20T11:21:15+00:00

I have a set of PowerShell scripts that include a common script, located in

  • 0

I have a set of PowerShell scripts that include a “common” script, located in the same folder, like this:

# some-script.ps1
$scriptDir = Split-Path -Parent $myinvocation.mycommand.path
. "$scriptDir\script-utils.ps1"

This is fine if the script is called directly, e.g.

.\some-script.ps1

However, if the script is called with Invoke-Command, this does not work:

Invoke-Command -ComputerName server01 -FilePath "X:\some-script.ps1"

In this case, infact, $myinvocation.mycommand contains the contents of the script, and $myinvocation.mycommand.path is null.
How can I determine the script’s directory in a way that works also when the script is invoked with Invoke-Command?

NOTE
In the end, this is the solution I actually used:

Invoke-Command -ComputerName server01 `
  {param($scriptArg); & X:\some-script.ps1 $scriptArg } `
  -ArgumentList $something

This also allows passing parameters to the script.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:21:16+00:00Added an answer on May 20, 2026 at 11:21 am

    I don’t believe you can, from within the invoked script. From get-help invoke-command:

    -FilePath
    Runs the specified local script on one or more remote computers. Enter the path and file name of the script, or
    pipe a script path to Invoke-Command. The script must reside on the local computer or in a directory that the
    local computer can access. Use the ArgumentList parameter to specify the values of parameters in the script.

     **When you use this parameter, Windows PowerShell converts the contents of the specified script file to a script
     block, transmits the script block to the remote computer, and runs it on the remote computer.**
    

    When you use invoke-command using the -filepath parameter, the script is read from the file on the local computer, converted to a script block, and that’s what gets passed to the remote computer. The remote computer doesn’t have any way of knowing if that script block was read from a file.

    For the remote computer to know what that original file path was, you’ll have to tell it. I think the easiest way to do that would be to write a function to do the invocation, and have it pass the filename to the invoked script as a parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a powershell build script that calls out to other scripts that is
Does anyone have a handy powershell script that gets a set of files from
I have a PowerShell script located at D:\temp . When I run this script,
I'm writing a powershell v2 script that I'd like to run against a remote
I have a powershell script that modifies transport rules. It works perfectly from powershell
I have a powershell script that is replacing patterns in a file with the
I have a simple powershell script that gets ran daily to compress and move
I have set up a Django application that uses images. I think I have
I have set of scripts for doing scripted installs. You can use the scripts
I have a need to kick off a Data Collector Set that I've predefined.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.