Create a Scheduled Task to keep all Chocolatey Packages up-to-date

Rebuilding your PC is always a drag, even with useful utilities like Ninite.

I recently created a PowerShell DSC script that I can use whenever I need to rebuild my PC. As part of that, I used the cChoco provider to automatically install applications using Chocolatey. I’ll be writing a blog post with more details shortly.

That’s a great way to get the applications installed, but not for keeping them up-to-date. Chocolatey allows you to run ‘choco upgrade all’ manually to do this:

Command prompt window showing the results of a 'choco upgrade all -y' command

Rather than manually create the scheduled task to automate this, I created this short PowerShell script:

# See if choco.exe is available. If not, stop execution
$chocoCmd = Get-Command Name 'choco' ErrorAction SilentlyContinue WarningAction SilentlyContinue | Select-Object ExpandProperty Source
if ($chocoCmd -eq $null) { break }
# Settings for the scheduled task
$taskAction = New-ScheduledTaskAction –Execute $chocoCmd Argument 'upgrade all -y'
$taskTrigger = New-ScheduledTaskTrigger AtStartup
$taskUserPrincipal = New-ScheduledTaskPrincipal UserId 'SYSTEM'
$taskSettings = New-ScheduledTaskSettingsSet Compatibility Win8
# Set up the task, and register it
$task = New-ScheduledTask Action $taskAction Principal $taskUserPrincipal Trigger $taskTrigger Settings $taskSettings
Register-ScheduledTask TaskName 'Run a Choco Upgrade All at Startup' InputObject $task Force

The script will:

  1. Locate the choco.exe binary (It’ll quit if it can’t find it in the path)
  2. Set up a scheduled task that runs said binary at system startup

Note that this script will only work on Windows 8 and newer machines, because it relies on the *-ScheduledTask cmdlets.

Quick Tip: Run PowerShell elevated as a domain admin

If, like any sane sysadmin, you adhere to best practice and your own user account isn’t a domain admin, you’re likely to be running certain operations as a domain admin.

I’ve used batch files to run CMD, MMC, and other applications as a domain admin account in the past. To run PowerShell, I had to use a slightly different method.

1. Create a PowerShell script that contains the following, and save it somewhere:
Start-Process powershell.exe -Credential "domainadministrator"

2. Create a shortcut on your desktop or similar, with the following path:
%SystemRoot%system32WindowsPowerShellv1.0powershell.exe -File C:pathtoscriptRun-PowerShell-Elevated.ps1

3. Optionally, set the shortcut icon to one of the ones contained in the PowerShell exe. I used the UAC one.

To test that it worked, run the script, authenticate, and then do the following:
Set-Location env:

You should see that the username matches the one you started the process as.

7-Zip tip – Quickly extract into a particular folder

If you need to extract a file into a particular folder, what do you normally do? Some people may copy the Zip file into the destination folder, and then unzip it. Others may launch their unzip tool of choice’s file manager and use the unzip wizard to point to the destination folder.

7-Zip provides a handy way to do this. All you need to do is right-mouse-drag the Zip file to the destination folder, release the right-mouse button, and go to 7-Zip –> Extract … from within the context menu.

20-10-2011 101712

20-10-2011 101905

I’d been using 7-Zip for years before I figured this one out.