We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


PowerShell No File Output with Task Scheduler

Medium Priority
Last Modified: 2012-05-06
I have a PowerShell script that checks the disk space on various servers in my network and then emails me a result file.  I have a command (.CMD) file to run the script. It works fine if I execute it at the PowerShell command line.  If I run the same .CMD file from within  a task scheduler task I get a blank e-mail.  So the shell script is executing but with no result.  See the code section below for details.  Notice that I am capturing the console output to a file in the command file.  This works fine when executed at the command line.  But the task scheduler does not create that console output file.  Also - The task scheduler is set up to run with domain admin privileges, the same as my login used at the command line.  This issue is driving me batty.  Any help is appreciated.  

Added additional error handling code to better determine what is going on.  See revised PS script below.  The email now contains the single line: "Drive list query returned empty with no errors"
########  The .CMD file:
@echo off
Powershell.exe -command "& 'c:\scripts\DiskSpace.PS1'" > DiskSpaceTranscript.log
######## And the PowerShell script:
$logfile = "C:\Scripts\DiskSpace.log"
## Setup log file e-mail
$smtpServer = "foo.com"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "foo@foo.com"
$msg.Subject = "Disk Space Less Than 30GB"
New-Item -ItemType file $logfile -Force
## List any server that has a disk which free space is less than 30GB
$driveList = get-wmiobject -computer (get-content servers.txt) -query "Select * from win32_logicaldisk where freespace <= 32212254720 and drivetype=3" -errorvariable myerror -erroraction silentlycontinue
if (-not $driveList)
	if ($myerror)
		add-content $logfile "Drive list query returned empty with error:" 
		add-content $logfile $myerror[0].categoryinfo
		add-content $logfile "Drive list query returned empty with no errors"
	format-table -inputobject $driveList -groupby SystemName @{label = 'Server'; expression = {$_.SystemName}; width=12}, 
		@{label='Drive'; expression = {$_.DeviceID}; width=7}, 
		@{label = 'FreeSpace'; expression = {"{0:n0}MB" -f ($_.FreeSpace / 1mb)}; width=10} | out-file $logfile
$msg.Body = [string]::join([environment]::newline, (get-content $logfile))

Open in new window

Watch Question

First... so you know you can call Powershell directly from Task Manager. Copy you code to .ps1 file and add the task using: Powershell.exe -command <ScriptFile> -noprofile

Second... I suspect your problem is an authentication issue. When you run a schedule task it often runs as system. The system account does not have access to remote machines.


As I stated in the original question, the task is set up to run as domain administrator.  Is there some policy I need to set for logins in Domain Administrators group that allows access to remote machines?

One way to make sure is to add this to the top of the Powershell script.

$NTIdentity = ([Security.Principal.WindowsIdentity]::GetCurrent())

btw... I do not see mention of the OS you are running the script from.


The zone states Windows Server 2008, which is the server I am running this on.
I will try logging the $NTIdentity as you suggest.  If it is indeed using the domain admin login what else could cause this?  IS there a policy setting?


So I added the output of the logged in username to the script like so:
$loggedInUser = ([Security.Principal.WindowsIdentity]::GetCurrent())
out-file -filepath $logfile -append -inputobject $loggedInUser.name
and ran it using the task scheduler.  Sure enough the account is my domain administrator account.
I do not understand why this account would not have the privileges necessary to run this script?
Thanks for the help.
I agree... it is odd. can you write the error stream out?

$error | out-file errorlog.txt -enc ascii

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Bingo!  That single line of how to write out the current error pointed me in the right direction, a million thanks!
The problem was that the call to "get-wmiobject -computer (get-content servers.txt) " was failing because when executing the ps1 script from Task Scheduler the "current directory" was no longer the scripts directory.  Task Scheduler executes out of c:\windows\system32 so it was expecting the servers.txt file to be in that directory.  Doh.


Really appreciate the help and the fact you sticked with me through this.

Glad to help
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.