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 7731823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:34:37+00:00 2026-06-01T06:34:37+00:00

I need to run a ps1 script on a remote computer. The script will

  • 0

I need to run a ps1 script on a remote computer. The script will be passed a single parameter representing a file name. I have tried many, many different combinations and approaches to passing the parameters to the script block, but I am always getting one error or another when trying to support spaces in the script name and/or file name.

Note: The end result will be running a script on a REMOTE computer using the -ComputerName parameter to Invoke-Command, but for simplicity and testing all the examples run locally.

Given the sample “remote” script

#processFile.ps1
$args[0]     # Just outputs the first parameter passed

The following works when there are no spaces in the name

$cmd = ".\processFile.ps1"
$fn = "someFile.csv"
$sb = [ScriptBlock]::Create("$cmd $fn")
Invoke-Command -ScriptBlock $sb

# Outputs the correct
someFile.csv

However, the following does not work

$cmd = ".\processFile.ps1"
$fn = "some File.csv"
$sb = [ScriptBlock]::Create("$cmd $fn")
Invoke-Command -ScriptBlock $sb

# Outputs the incorrect
some

Obviously, the file name parameter needs to be escaped and passed as “some File.csv”. And this can be done with the following:

$cmd = ".\processFile.ps1"
$fn = "some File.csv"
$sb = [ScriptBlock]::Create("$cmd `"$fn`"")   # Notice the escape $fn parameter
Invoke-Command -ScriptBlock $sb

# Outputs the correct
some File.csv

BUT, when I try to extended this space support to the script name, everything falls apart. The following fails

$cmd = ".\processFile.ps1"
$fn = "some File.csv"
$sb = [ScriptBlock]::Create("`"$cmd`" `"$fn`"") # Notice the attempt to escape both parameters

with the following exception

Exception calling "Create" with "1" argument(s): "Unexpected token 'some File.csv' in expression or statement."

Everything I have tried, and there have been many, many different approaches, have resulted in something similar. I just cannot seem to get both parameters to be escaped. (Actually, the problem is more than I cannot escape the first parameter. Some of my attempts have included nested quotes, single quotes, [ScriptBlock]::Create, { }, $executionContext, etc.

  • 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-06-01T06:34:39+00:00Added an answer on June 1, 2026 at 6:34 am

    Try this:

    $sb = [ScriptBlock]::Create(@"
    &'$cmd' '$fn'
    "@)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to run a script and have access to the default stdin (terminal
I need to run a PS script on a remote machine, am doing as
i need run code that will create a database and populate tables. i am
I need to run a standalone ruby script as Unix (linux) daemon. After running
I need to run a windows command line tool from a php script on
I need run a system call to run a .app file from java. Any
I need to run a bash script and a perl program in detached background
I need to run an exe file that is located on an network drive
I'm building a Flex application that will need run under two different deployment scenarios:
I need to run a bash script as root (passwordless sudo or su not

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.