how can i schedule this exchage shell command

Get-Mailbox -OrganizationalUnit "OU=3rd Party Contractors,DC=company,DC=co,DC=uk" | Set-Mailbox -CustomAttribute1 "ExcludeFromAllUsers"

I have tried several commands on internet but nothing works
LVL 1
dougdogAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DLeaverCommented:
Have you tried saving it as a .ps1 file and running it from task scheduler within Windows?....
dougdogAuthor Commented:
yes but cant seem to get the commands right
DLeaverCommented:
What errors are you seeing?...
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Thomas GrassiSystems AdministratorCommented:
You need to load the exchange powershell set first

create yourself a batch file that will call your powershell script and will load the exchange shell also

This is how I do it

@echo off
cd\util
PowerShell.exe -PSConsoleFile "D:\Program Files\Microsoft\Exchange Server\V14\Bin\ExShell.psc1" -Command "./EASDevicestats.ps1"
exit


This works then you can add the batch file to your task schedular

Of course you can name your powershell what ever you want

HTH
dougdogAuthor Commented:
do you create a util folder on the c drive and copy ps1 script file into it?
Thomas GrassiSystems AdministratorCommented:
yes you can or what ever folder you wish to use

put the ps1 and cmd file in the same folder
dougdogAuthor Commented:
Thomas Thats bang on perfect i have been messing about for ages trying to get this to work
Excellent thank you so much

would it be hard to get this task to email success /failure??
Thomas GrassiSystems AdministratorCommented:
It will be successful for sure

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dougdogAuthor Commented:
perfect thanks again appreciate it
Thomas GrassiSystems AdministratorCommented:
Glad I was able to help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.