1

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.

1 Answers1

1

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
Graham Gold
  • 2,435
  • 2
  • 25
  • 34
  • You can enter file name without extension for `.ps1` file and any file type mentioned in `PATHEXT` environment variable (which by default is `.com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh;.msc`). – user4003407 May 03 '15 at 08:28
  • Yep but you have to tell powershell what to do with it - the bare minimum even with `.bat` in your path would be to prefix with `.` to dot source it e.g `.\aws` – Graham Gold May 03 '15 at 09:34
  • I tried both solutions but I get messages saying commands are not recognized. However when I type them into the Powershell window directly they are okay. –  May 03 '15 at 10:58
  • Can you please post the content of `aws.bat` - it looks like you are trying to execute powershell commands in an nt batch script – Graham Gold May 03 '15 at 11:01
  • No, you do not have to tell PowerShell "what to do with it". You can run executables and batch files and others, as @PetSerAl suggests, without suffix and without an ampersand prefix. You were close when you suggested you need to dot-source it--but that is only to include it in the current scope. Without dot-sourcing, it is run in a child scope, so variables within are not available to the current scope after the batch file completes. – Michael Sorens May 04 '15 at 11:44