Calling powershell cmdlets from Windows batch file

后端 未结 6 1148
青春惊慌失措
青春惊慌失措 2020-12-08 21:14

Ok something so simple is just not working for me. I got a cmdlet that accepts a single parameter. I am trying to call a cmdlet within a Windows batch file. The batch fil

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 21:45

    I explain both why you would want to call a PowerShell script from a batch file and how to do it in my blog post here.

    This is basically what you are looking for:

    PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\convert-utf8-to-utf16.ps1' 'C:\test.txt'"
    

    And if you need to run your PowerShell script as an admin, use this:

    PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\convert-utf8-to-utf16.ps1"" ""C:\test.txt""' -Verb RunAs}"
    

    Rather than hard-coding the entire path to the PowerShell script though, I recommend placing the batch file and PowerShell script file in the same directory, as my blog post describes.

提交回复
热议问题