The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script block, or a CommandInfo object.

Michael Ocasio 20 Reputation points
2024-07-03T18:24:25.33+00:00

Im executing the following code from windows bat file

%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%' -fileControl 'C:\HHSWHRS\sqlloader_scripts\ld_owcpcomp.ctl' -placeValue 'datevalue'"

what is causing the above error?

any ideas thanks for the suggestions

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,710 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue 37,621 Reputation points Microsoft Vendor
    2024-07-04T02:53:30.95+00:00

    Hi,

    The bat script runs as expected on my machine so I suppose it's a problem with the PowerShell script.User's image

    Can you post the the PowerShell script? Do you see the error message if you run the PowerShell script from Windows PowerShell?

    Also see if this works.

    @ECHO OFF
    PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%~dp0\MyPowerShellScript.ps1' -Param1Name 'Param 1 Value' -Param2Name 'Param 2 Value'"
    

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Marcin Policht 25,675 Reputation points MVP
    2024-07-03T19:03:15.7466667+00:00

    Try the following:

    @echo off
    set PowerShellScriptPath=C:\Path\To\Your\Script.ps1
    %SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%' -fileControl 'C:\HHSWHRS\sqlloader_scripts\ld_owcpcomp.ctl' -placeValue 'datevalue'"
    
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    1 person found this answer helpful.

  2. Michael Ocasio 20 Reputation points
    2024-07-03T20:48:00.2033333+00:00

    I had that variable set before but decided just to send you the error. I will send it later.

    thanks for the help

    @ECHO OFF

    SET ThisScriptsDirectory=%~dp0

    SET PowerShellScriptPath=%ThisScriptsDirectory%MyPowerShellScript.ps1

    PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%' -Param1Name 'Param 1 Value' -Param2Name 'Param 2 Value'"

    I got it from here

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.