Search code examples
powershellamazon-web-servicesamazon-s3aws-powershell

How do I execute a script from Windows Powershell?


I created a small aws.bat script and then in PowerShell I navigated to the script directory.

But when I type aws then I get an error message and it does not execute the script. Is there a special thing I should be doing?

Here's what I have tried:

PS C:\G> .\aws.bat

C:\G>$BucketName = "ab-user"
'$BucketName' is not recognized as an internal or external command,
operable program or batch file.

The very first and every othre command has a similar message.


Solution

  • You can't just enter the name on its own, with or without file extension, you first have to tell powershell what to do with it.

    You can call it as follows:

    & .\aws.bat
    

    If you need the LastExitCode returned in powershell to check if the bat file worked (0 usually means it worked, anything else usually means it didn't), use Start-Process as follows:

    $batch = Start-Process -FilePath .\aws.bat -Wait -passthru;$batch.ExitCode
    

    And if you want the batch file to be hidden when it is run by powershell do this:

    $batch = Start-Process -FilePath .\aws.bat -Wait -passthru -WindowStyle Hidden;$batch.ExitCode