VS 2022 - PowerShell script in Pre-build event field

Jim Jupiter 40 Reputation points
2025-03-12T18:44:45.7766667+00:00

Hi,

I would like to put a script in the pre-build event field but it does not work and

I can't see why - out of VS direct in PowerShell the script works

I put this text script in the field and got error 9009

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -Command "

Write-Host 'Hello from PowerShell!'

"

got this

$content = Get-Content "build.txt"

$parts = $content -split " "

$major = [int]$parts[0]

$minor = [int]$parts[1]

$patch = [int]$parts[2]

$build = [int]$parts[3] + 1

got error "The command '$content/parts/major' is either misspelled or could not be found."

Any help would be great

Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
1,033 questions
0 comments No comments
{count} votes

Accepted answer
  1. Dou Xu-MSFT 400 Reputation points Microsoft External Staff
    2025-03-13T06:25:09.5666667+00:00

    Hello Jim Jupiter ,

    Welcome to Microsoft Q&A forum.

    Error code 9009 do indicate the command is not recognized. We can know that after typeing a PowerShell command in build events, VS calls cmd.exe to execute it. In your case, it seems that cmd.exe can't recognize a PS command(including Line breaks) and throw error.

    Please remove line breaks and write the command on one line.

    "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -Command "Write-Host 'Hello from PowerShell!'"
    

    Then copy and past to Pre-Build event filed.(To make it easier to read, you can click on Word-wrap )

    enter image description here

    Test result comparing: Failed

    enter image description here

    Succussed enter image description here

    got error "The command '$content/parts/major' is either misspelled or could not be found."

    Please try modify them like the following: Adding this content "This is a Pre Build Test" in build.txt for test.

    "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Command "$content = Get-Content 'build.txt'; $parts = $content -split ' '; $major = $parts[0]; $patch = $parts[2] ;$build = $parts[3] + 1;$content;$parts;$major;$patch;$build"
    

    enter image description here

    Best Regards, Dou


    If the answer is helpful, please click "Accept Answer" and upvote it. Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.