Powershell: Using SCHTASKS in Powershell


In this post you will see how powerful powershell is in combination with external applications like SCHTASKS.exe.

First you have to create a CSV file like this:


The following one-liner imports the CSV file and creates a task on every server which is saved in the CSV file:

# Create a scheduled task on all the servers in *.csv
import-csv ".\*.csv" | % { schtasks /create 
/S $_.Name /SC DAILY /TN "Task_Name" 
/TR "program_or_script" /ST time /RU account}

The next one-liner imports the CSV file and will modify a scheduled task on every server which is saved in the CSV file:

# Change a scheduled task on all the servers in *.csv
import-csv ".\*.csv" | % { schtasks /change 
/TN "Task_Name" /S $_.Name 
/TR "program_or_script" /ST 23:05 /RU System }

The last one-liner imports the CSV file and will delete a scheduled task on every server which is saved in the CSV file:

# Delete a scheduled task on all the servers in *.csv
import-csv ".\*.csv" | % { schtasks /delete 
/tn "Task_Name" /f /s $_.Name }

More info about how to use SCHTASKS.exe can be found here: KB814596

Powershell: Delete a Folder of a Particular Age


I created this script to delete a folder which is older than two days. I needed this for a simple backup solution. I have a Powershell script that will backup a VM which runs in VMware Workstation. This script runs everyday and creates a folder with a date stamp. After a couple weeks there where a lot of copies of the particular VM and the free space of the backup hard disk became very low.

This script will remove all folders in the D:\vmbackup folder which are older then 2 days.

$Now = Get-Date
$Days = "2"
$TargetFolder = "D:\vmbackup"
$LastWrite = $Now.AddDays(-$Days)

$Folders = get-childitem -path $TargetFolder | 
Where {$_.psIsContainer -eq $true} | 
Where {$_.LastWriteTime -le "$LastWrite"} 

    foreach ($Folder in $Folders)
    write-host "Deleting $Folder" -foregroundcolor "Red"
    Remove-Item $Folder -recurse -Confirm:$false

Powershell: Set Logon Hours for all the users in an OU


With the script in this post you’re able to set logon hours to a bunch of users. All you have to do is to setup logon hours for a “template” user and define this “template” user into the $template variable.


The other step is to define the $ou variable with the path to the OU. In my case this was ict-freak/Gebruikers.

The script will now read the default logon hours and will apply them to the users in the OU.

$template = "" # This is a user with the default logon hours
$ou = "" # the full path to your ou "domainname/ouname1/ouname2"

# Get the logonhours from the template user
$template = Get-QADUser $template -IncludedProperties logonhours
[array]$logonHours = $template.DirectoryEntry.logonHours

# Get all users
$users = Get-QADUser -OrganizationalUnit $ou

# Loop through all the users
foreach($user in $users){
    Set-QADUser $user.Name -oa @{logonHours = $logonHours}


I found this trick here: http://www.powergui.org/thread.jspa?threadID=7860

Windows Update error code 80244019


On a fresh installed Windows Server 2008 X64 VM, a received the following error when I want to search for new Windows Updates:


The solution in my case was to delete the following registry key: 



Source: http://social.technet.microsoft.com/Forums/en-US/itprovistasetup/thread/e788027e-9292-47c0-891e-90309cf13367

Powershell: Working with Excel and error HRESULT: 0x80028018


If you want to use Microsoft Excel in your Powershell scripts.

$Excel = New-Object -Com Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()


You can run into the following error:


The work around for this issue was changing the Regional Options back to United States International.


Microsft created a work around. More info can be found in in: KB320369

guillermooo has created a port to Powershell. Just copy the following code and you should be able to open Excel and create a new workbook.

I didn’t test this script block so let me know if it works 😉

$ci = new-object system.globalization.cultureinfo "en-US"

$e = New-Object -COM "Excel.Application"
$e.Visible = $True
$e.UserControl= $True
$books = $e.Workbooks
$books.PSBase.GetType().InvokeMember( `
       "Add", `
       [system.reflection.bindingflags]::InvokeMethod, `
       $null, $books, $null, $ci)

Source: http://stackoverflow.com/questions/687891/exception-automating-excel-2007-with-powershell-when-calling-workbooks-add

How To: Uninstall Powershell 1.0


If you’re trying to install Powershell 2.0 CTP3 on a Windows PC with Powershell 1.0 already installed, the installation wizard will stop with the following warning.


Before the wizard will continue, you have to uninstall Powershell 1.0.

Open Control Panel. Select Show updates and browse to Windows XP – Software Updates. Click on Remove to uninstall Powershell 1.0.


If you don’t have an entry in the Add/Remove Programs control panel, You can find the Powershell 1.0 Uninstaller here:

  • C:\WINDOWS\$NtUninstallKB926139-v2$\spuninst\spuninst.exe
  • C:\WINDOWS\$NtUninstallKB926139$\spuninst\spuninst.exe

After uninstalling Powershell 1.0 you’re able to install Powershell 2.0 CTP3.

Posh: Run gpupdate on Multiple Computers


I created a Powershell script that will get all the computers from a certain OU and run GPUpdate /force on these machines. This script uses the Quest Active Directory cmdlets and PsExec.exe. You need to install the Quest cmdlets first. If PsExec doesn’t exist, the script will download it to the c:\tools directory.

When you start the script, you have to enter the FQDN and the OU name:


The script creates a text file with al the computer names in it. This text file will be used with the PsExec.exe command.


You can find the script on poshcode.org