Running a PowerCLI Scheduled task

There are a fair few PowerCLI scripts which can be run as scheduled tasks, some that email you or maybe a monitoring script but recently I have been asked how this can be done, what do you put in the Run line for the script to work properly.

There are two ways of doing this really:

1.  Add a line to a start of each of your scripts to make sure it loads the PowerCLI snapin, without which PowerShell will not recognise any of the PowerCLI cmdlets.

Add the following line to the top of each script you will be running as a scheduled task:

add-pssnapin VMware.VimAutomation.Core

Then in the run box on your scheduled task you can call your script with the following command:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe “& ‘C:\Scripts\MyScript.ps1′”

But remember when you are troubleshooting this script or amending it you will most likely get an error as your script editor will automatically add the VMware snapin.

2.  This method is the one I use as its easier than remembering to put the line at the top of each file and then commenting it out when you want to edit it etc, this method runs the PowerCLI Console file before it runs your script.

A PowerShell Console file is a xml like file containing information on what snapins to load when PowerShell is started.

This will enable the snapin for you and there is nothing to ad to the scripts.

The run command then looks like this:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ ” &  “C:\Scripts\MyScript.ps1”

You can test both of these methods by running them from a cmd prompt before using it to ensure they work properly.

77 thoughts on “Running a PowerCLI Scheduled task”

  1. This worked for me 2008R2


    Add arguments (optional):
    -psc “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1” -noe -c “.\”C:\vcheck\vcheck.ps1\””

  2. Here’s the working version for 2012*

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -psconsolefile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1” -noexit -command c:\myscript.ps1

    If you’re on a 32-bit system, try this:

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -psconsolefile “C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1” -noexit -command c:\myscript.ps1

  3. For some reason this command works when I run it from a command prompt, but doesn’t seem to start the script (I added a line to create a file at the start of my script, and it doesn’t even get to that) when run as a scheduled task. Any ideas why this might be?

  4. Hey the vcheck script is great with tons of useful information. However when I run this even though I have “set-executionpolicy unrestricted” I am still prompted for each script with the security warning whether I want to run once or not waiting for me to type r to run it, i can use bypass and skip the prompts. But I am trying to schedule this through the task scheduler and it is getting stuck I assume on waiting for the input. I have changed my task to have the following C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe and the following arguments -executionpolicy -bypass -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1” “& ‘E:\Scripts\VMWare\vCheck\vCheck.ps1′”. This results in the script saying it ran succesffuly in less than 60 seconds but nothing happens, no email no nothing. Any suggestions how to skip those security warning prompts.

  5. Ok I actually got this working in Task Scheduler on Windows Server 2008 R2 by doing the following




    -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ “& ‘C:\vCheck\vCheck.ps1′”

  6. So out of these possibilities, which are correct for putting in a Windows 2008 R2 Task Scheduler?

    C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ -noexit “& ‘C:\vCheck\vCheck.ps1′” my-vcenter

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ “& ‘C:\Scripts\VMDailyReport.ps1′ my-vcenter″

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ “& ‘C:\Scripts\VMDailyReport.ps1′” my-vcenter


    Program/script: C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe

    Arguments: -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ -command “& c:\vCheck\vcheck5.ps1 vcenterserver”

  7. I’m a beginner powershell/powerCLI user trying to use your suggestion above, and I’m having problems determining what special characters, ticks and others, that you are using within the quotation “& ‘C:\Scripts\MyScript.ps1′”. Can you explain or point me to a document that does.


  8. Great script!
    I may have found a minor typo on lines 385 and 387:

    The variable $VISRV should be $VIServer if you want the name of the vCenter server to show up in the subject line of the email.

  9. Last thing, replace manualy every ” with one you type in the command line. The Copy/Paste didn’t copy the good one (wp bug ? )

  10. For those like me who are in a bad mood with ” you can also used this syntax :

    powershell.exe -PSConsoleFile “%ProgramFiles(x86)%\VMware\Infrastructure\vSphere PowerCLI\vim.psc1” -File “C:\vCheck6\vCheck.ps1”

  11. Hi,
    I was wondering how I would run a VIEW PowerCLI script as a scheduled task?
    I do not have the VMware PowerCLI installed on my connection server, just the View PowerCLI, so ‘VMware.VimAutomation.Core’ is not available. Any ideas?


    1. The view snapin is called VMware.View.Broker so i would add something like this to the start of your script:

      if (!(get-pssnapin -name VMware.View.Broker -erroraction silentlycontinue)) {
      add-pssnapin VMware.View.Broker

  12. vCheck worked right away.

    The surprise was it find ‘exceptions’ and reports on them. I used it recently as we built a lab prior to a mgiration to later version of vSphere. It was helpful to monitor both production and lab with script (aimed at the different vCenters).

    It was easy to adjust limits (such as number of VMs per LUN up from 5 (default), etc).

    Nice job.

    Larry T.

  13. Trouble running as scheduled task resolved.

    I have been playing with this exceptionally helpful script for a few days as I was having trouble triggering it via a scheduled task in Windows 2008R2 and noticed others also had problems.

    I needed to change the line in the script that set the appdata environment variable from “C:\Documents and Settings\” + $USER + “\Application Data” to “C:\Users\” + $USER + “\AppData\Roaming”, I also hardcoded the cred file path to $CredFile = “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\mycred.crd” once created and tested.

    The correct syntax is throughout the posts above.

  14. Hello,
    Awesome vCheck script! Saves me so much time it is amazing. I do have one issue that maybe you have seen. When the html comes out to IE it looks perfect but when it sends to my email (outlook 2010) the secondary titles and descriptions and any background colors are lost. Is there anything I can do to clean up the look of the email output to match the IE output?

    Thanks again for an awesome script.


  15. I read all the comments and could not get this to execute on my Win7 x64 PC. I downloaded System Scheduler free, put the commands into a .bat file and it executed with no issue.

  16. Note on a 64bit server the path to vim.psc1 is different it should be “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1”

  17. this is all is says.
    [vSphere PowerCLI] C:\> cd scripts
    [vSphere PowerCLI] C:\scripts> cd test
    [vSphere PowerCLI] C:\scripts\test> .\vcheck5.ps1
    2:41:06 PM Connecting to VI Server
    [vSphere PowerCLI] C:\scripts\test>

  18. Try to run the task from a PowerCLI prompt and see what you get. Make sure the account you specified when creating the XML encrypted file has rights to log onto VCenter.

  19. i’ve comment out that line, started the scheduled task, a command box is opened and i see
    8:09:03 AM Connecting to VI Server
    PS C:\WINDOWS\system32\WindowsPowerShell\v1.0>

    and nothing happens

  20. I am experiencing an interesting problem … I run the script, it connects, gathers the information, gets to “Displaying the HTML Results” … and … nothing.

    I get no browser display … nothing in the event logs, I cannot see any processes that have started after the script (iexplore.exe for example).

    Anyone seen this before?

  21. Comment out param line which is at the very top of the script. If you do that and run the script what error do you get?

  22. Guido,

    Add the below to the very top of your script.Also please ensure your running the script with the same account you used to create the XML.

    add-pssnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

  23. I have tried the document you’ve made. made the xml file edited the vcheck5 file.
    But i still get the error that is above.
    The term ‘param’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
    try again.
    At C:\scripts\test\vCheck5.ps1:3 char:6
    + param <<<< ( [string] $VISRV)
    + CategoryInfo : ObjectNotFound: (param:String) [], CommandNotFoundException

  24. I get this error all the time, i run the script from powercli:

    The term ‘param’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if
    try again.
    At C:\scripts\test\vCheck5.ps1:7 char:6
    + param <<<< ( [string] $VISRV)
    + CategoryInfo : ObjectNotFound: (param:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

  25. well I guess I could just run it as a batch file and pass the user id information into the script and run that as a scheduled task… not my perferred method but I think it would work – a batch file for each environment would prob work. Sorry for the double post Alan.

  26. Hey Alan,
    Been busy day for me here at the office but I have been trying to figure out how to get this done. Here is a summary of what I am trying to accomplish.

    1.I have 9 vCenters and they all require different credentials. Where or how can I store these credentials and call them to run the script as a scheduled task. < forgive my ignorance 😉

    2. I dont need SMTP or anything like that I just want it to create HTML reports. I tried finding the path that it dumps the file in but have failed. I also use Powergui not sure if that will help with anything.

    I will more than likely run the scheduled task from my desktop since it has full access to all the different virutal environments over a vpn.

    I tried following the details in the script about a credential store and specifying a user name but that doesnt seem to work…

    Chad King

  27. I got the script to work with Win2008 R2 task scheduler by setting up the task as follows:

    Program/script: powershell.exe
    (i suggest to extend the path variable with %SystemRoot%\system32\WindowsPowerShell\v1.0\)

    Arguments: -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1” -command “& c:\scripts\vcheck5.ps1 vcenterserver”


  28. Hey guys,

    For the Windows 7 (64-bit) users, I finally found the syntax in order to run your scripts. Here it is:

    Program/script textbox:

    Add arguments textbox:
    -psc “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1” -command “C:\YourScript.ps1”

    deercutter, I hope you find this useful! I know I am relieved! I will be posting this on my blog (

  29. If I issue each command seperately, the script runs. But If I run the script together, it never gets by the first line:

    Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
    C:\PSscript\vCheck521.ps1 vi_server

    I had no problem runniing this script from a Windows XP workstation. Now I have a Windows 7 (64-bit) workstation.

    Any advice would be appreciated.

    Note* This was my previous script on a Windows XP workstation:

    %SystemRoot%\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1” -command “”

    Here is the error message it returns:

    Failed to load Windows PowerShell console file “C:\Program Files\VMware\Infrastr
    ucture\vSphere PowerCLI\vim.psc1”. WIN 32 error: The system cannot find the path

  30. I also have the same problem as Nicolas.
    here are my windows 2008 schedule task settings:
    Add arguments (optional):
    -noninteractive -noprofile -command “(ampersand) (open braket)C:\Scripts\vcheck\vcheck.ps1 vcenter1(closing braket)”

    Added the Vmware snapin at the begining of the script.

    The script runs but never ends enven after 24 hours.
    Interesting enough if I run the same command in a windows prompt It will complete without any issues

  31. This does not work on Server 2008 R2. I put the entire command in a PS script and then use the scheduled task to call the script to run. It runs, however never terminates or completes.

  32. Hello I was trying to get this script to run as a Task in Windows and have been having some issues. Here is what I am using:

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ -noexit “& ‘C:\VMScripts\vCheck5.ps1′” DITSFAVC01

  33. Daren:

    I found on Windows 2008 R2 simply entering the following in the two fields fixed it for me.
    Add arguments (optional):
    Note: removing the & (ambersand) with also removing the speak marks from the script posted above
    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe “& ‘C:\Scripts\MyScript.ps1′”

    This has also worked for me. I added “add-pssnapin VMware.VimAutomation.Core” on the first line of my script.

  34. Hey, great script!
    But I got one problem: When I run the script vom cmd-line everything is fine, I passed the connection Info like written in “Adjusting connection information”, so no user-request here.
    When I run the command as a scheduled task, I get asked for user and password. If I’m logged into the running user, I can click “cancel” multiple times and get a report at the end. But of course the user normally isn’t logged in and the script won’t go on without interaction.

    Any solution?


  35. Please disregard my last post. I have successfully run the script interactively thank you now to set it up as a scheduled task.

  36. Hi, Thanks fro this script bit I cannot get it to work. I am actually trying this on ESX 3.5 VC 2.5 U2 and in your example you have the command line …“C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″

    I actually don’t have that file (vim.psc1) in that path, or any other path for that matter. I do have the vSphere RCLI components installed, powershell installed, and .NET installed. So it would appear I have all the dependencies. On a brighter note the email component does work as I am receiving error emails.

    So if someone could kindly steer me in the right direction I would appreciate it.

  37. I could not get it to run while logged out until I put it in a .cmd. So now I have a SchedTask calling vCheck.cmd, which contains:
    C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1” “& ‘E:\vCheck\vCheck5.ps1′” my-vcenter

  38. After many attempts, realized that -noexit need to be after -PSConsoleFile

    C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1” -noexit “& ‘E:\vCheck\vCheck5.ps1′” my-vcenter

  39. Hi and Thanks again for doing all this work. I finally got it running the way I want it.

    Here is a correction to post #5, you had the vcenter server inside the quotes and it wasn’t working. It should work with this syntax:

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1″ “& ‘C:\Scripts\VMDailyReport.ps1′” TESTVC01

  40. I found on Windows 2008 R2 simply entering the following in the two fields fixed it for me.


    Add arguments (optional):

    Note: removing the & (ambersand) with also removing the speak marks from the script posted above

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe “& ‘C:\Scripts\MyScript.ps1′”

  41. You can just add the line to your script and avoid the error:

    Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

    I personally find this way easier.

  42. Hi
    just to let you know that the following script doesn’t run, could you please help my give more clear instruction as how to make it work.

    I tried to run this script from vSphere PowerCLI on my desktop. and I simply replace MyScript.ps1 with my own script called “DailyReport.ps1”. I got error, could you please help.

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ ” & “C:\Scripts\MyScript.ps1″


    hope to hear from you soon.

    Best Regards,

    Steve Chung

  43. @Chrisfoo
    Hi, the above cmd line is to be used in a scheduled task window, if you are trying to run this from the powershell prompt then you can just CD into the folder and do:

    .\DailyReport.ps1 YOURVISERVER

    As you have custom connection ports and credentials you will need to edit the script manualy and find the line which does a connect-viserver and adjust it to work as specified in your comment.

    Hope this helps.

  44. Hi There, I am new to powershell and having a few issues with getting this DailyReport to work the ways that have been mentioned in sch tasks and command prompts,
    Our Vcenter uses a different Port
    1. I have to first do this: Proxy stops Connection: Set-VIToolkitConfiguration -ProxyPolicy NoProxy

    I can then connect with this: Connect-viserver -server vcenter server-Port 1443 -Credential (Get-Credential servername\administrator) (THIS CONNECTION WORKS)

    But if i try and run the suggestions at the thread above i get this
    [VI Toolkit] C:\Program Files\VMware\Infrastructure\VIToolkitForWindows> C:\WIND
    OWS\systems32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\ProgramFi
    les\VMware\Infrastructure\VIToolkitForWindows\vim.psc1” “& ‘C:\Scripts\DailyRepo
    rt.ps1 vcenterserver -Port 1443”
    The term ‘C:\WINDOWS\systems32\windowspowershell\v1.0\powershell.exe’ is not re
    cognized as a cmdlet, function, operable program, or script file. Verify the te
    rm and try again.
    At line:1 char:59

    I can VI connect using Ecoshell gui to vcenter also, but how do you run the DailyReport from there?

    Any help would be much appreciated as i would like to be able to manage vcenter with Powershell

  45. @Virtu-Al

    That seems to have sorted it – although I already had the VMWare stuff loaded (it “complains” if I run it interactively) but I’m not goiing to worry!

    The script is excellent – thanks for all your help!!


  46. @slimboyfat
    Ok, Try this:

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1″ “& ‘C:\Scripts\VMDailyReport.ps1′ TESTVC01″

  47. Maybe I’m missing something, but scheduling doesn’t seem to work?

    I’ve created a batchfile as follows:

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -command “& ‘C:\Scripts\VMDailyReport.ps1’ VI-ServerName”

    If I run it interactively, it’s fine and I get the report – but if I try to schedule it, no report arrives….

    Any suggestions??

Leave a Reply