Coming from this question here: How to create a script for Exchange Powershell to modify settings for all shared mailboxes?

Here is my Powershell script (scroll right to see all):

$Mailboxes = Get-Mailbox -RecipientTypeDetails SharedMailbox

Foreach ($Mailbox in $Mailboxes) { 
    Set-Mailbox $Mailbox -MessageCopyForSentAsEnabled $True -MessageCopyForSendOnBehalfEnabled $True

It works fine on its own if launched from an Exchange Management Shell window.

But I want to launch it from a batch file, so I found the following Microsoft page: https://technet.microsoft.com/en-us/library/bb123798%28v=exchg.150%29.aspx?f=255&MSPPError=-2147217396#RunScript

Following the guide there, I created this batch file (scroll right to see all):

PowerShell.exe -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; 'C:\Program Files\Microsoft\Exchange Server\V15\bin\enablesentforshared.ps1'"

One thing I added which was not specified in the instructions is single quotes around the full path to the script I want to run. Without the single quotes I was getting an error about C:\Program not being a recognized cmdlet or script.

Anyway, when I run this batch file, the terminal window opens, then changes to a Powershell window, and the path name to the script gets printed out to the screen, but it prints out BEFORE the [PS] prompt, and nothing actually gets executed.

This is what my Powershell window looks like after executing the batch file:

VERBOSE: Connecting to exchangeserver.domain.com
VERBOSE: Connected to exchangeserver.domain.com
C:\Program Files\Microsoft\Exchange Server\V15\bin\enablesentforshared.ps1
[PS] C:\Users\Admin.domain>

And there is no indication that the script has actually run.

So what have I got wrong here? I tried changing the . to a & based on some googling I did, but then that gives me an error about Connect-ExchangeServer being an unrecognized command, so I think the . is closer to being correct.

  • That looks like it should work. Does your script have any output when you run it from powershell normally?
    – David
    Aug 26, 2016 at 5:00
  • So, there is definitely an output. Since I've run the script successfully before (without the batch file), then whenever I try to run the script again I get a bunch of notices that there are no settings to change (because I already changed them). But anyway you got me thinking... since I have the script in the Exchange Server \bin directory, I don't use the full path when running from the powershell terminal, just the name enablesentforshared.ps1. It works like this from the powershell terminal. So, I tried removing the path from the batch file, and it also works now!
    – Daniel
    Aug 26, 2016 at 5:25
  • Also, if I try running the full path, even from the powershell terminal, C:\Program Files\Microsoft\Exchange Server\V15\bin\enablesentforshared.ps1 it exhibits the same behavior as the batch file, telling me that C:\Program is not recognized. So if I try putting quotes at the terminal, like 'C:\Program Files\Microsoft\Exchange Server\V15\bin\enablesentforshared.ps1' or "C:\Program Files\Microsoft\Exchange Server\V15\bin\enablesentforshared.ps1", it again has the same behavior as the batch file, just spitting the full path back out at me and not actually running the script. Strange.
    – Daniel
    Aug 26, 2016 at 5:27

3 Answers 3


Try this (added dotsourcing of the second script):

PowerShell.exe -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; . 'C:\Program Files\Microsoft\Exchange Server\V15\bin\enablesentforshared.ps1'"
  • Yup, that little . did the trick...
    – Daniel
    Aug 26, 2016 at 14:42

Bat file script including send mail parameter. This bat file work on my window server 2016 and exchange server 2016.

PowerShell.exe -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; . C:\Script\Get-ExchangeEnvironmentReport.ps1 -HTMLReport C:\Script\Get-exchangeReport.html -SendMail:$true -MailFrom:Admin@domain.com -MailTo:user1@domain.com -MailServer:mail.domain.com"

I do this in a BAT file:

powershell -psconsolefile "C:\Program Files\Microsoft\Exchange Server\V15\Bin\exshell.psc1" -noexit "& Get-Mailbox | Get-MailboxStatistics" pause

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.