Categories
Welcome Center
Education & Development
Discussions
Certification Preparation
Recent Posts
Groups
Free Resources
Ebooks
Free Workshops
Trending Certifications Infographic
Infosec Training
IT & Security Training
Live Boot Camps
Security Awareness Training
About Infosec Institute
Home
Discussions
Off Topic
Scheduling Powershell script
mikedisd2
I can't get the syntax right on this one. I need to execute an Exchange Powershell script from the command line;
PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1" & 'c:\scripts\Report.ps1'
Running this in DOS returns the following:
Windows PowerShell
Copyright (C) 2006 Microsoft Corporation. All rights reserved.
PS C:\>
My PS book hasn't arrived yet. What am I doing wrong?
Find more posts tagged with
Save $250 on 2025 certification boot camps from Infosec!
Book now with code EOY2025
Button
Comments
Pash
What is your Execution policy set to?
From shell:-
Get-Executionpolicy
mikedisd2
The ExecutionPolicy was set to RemoteSigned; I've changed it to unrestricted but no change.
After running the command from DOS and then exiting the
PS C:\>
prompt, it states "The system cannot find the path specified." It's not recognising the path I'm providing even though it's vaild.
Running
& 'c:\scripts\Report.ps1'
or
& '.\scripts\Report.ps1'
from the PS prompt executes ok but not from the single DOS command.
Claymoore
Instead of using the -PSConsoleFile switch, try adding the Exchange Snap-in inside your PowerShell script:
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
Wait, when copying your code I just realized you aren't using the -command switch when specifying the PowerShell script.
Try:
PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1"
-command
& 'c:\scripts\Report.ps1'
Or:
PowerShell.exe
-command
& 'c:\scripts\Report.ps1'
If you include the Exchange Snapin inside the PowerShell script
mikedisd2
Got it sorted, it's a case of quotation marks after the -command. The working executable is:
PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1" -command
"
& 'c:\scripts\MailboxSizeReport.ps1'
"
Thanks for the help; Add-PSSnapIn is a also new one for me to know and love.
Quick Links
All Categories
Recent Posts
Activity
Unanswered
Groups
Best Of
INFOSEC Boot Camps
$250
OFF
Use code
EOY2025
to receive $250 off your 2025 certification boot camp!
BROWSE BOOT CAMPS