Skip to content

Instantly share code, notes, and snippets.

@rbleattler
Last active February 16, 2021 21:27
Show Gist options
  • Save rbleattler/4425d5af7dbb03d23f92a64a139ba8b7 to your computer and use it in GitHub Desktop.
Save rbleattler/4425d5af7dbb03d23f92a64a139ba8b7 to your computer and use it in GitHub Desktop.
This script will get ALL locally installed Chocolatey Packages, and upgrade ALL of them (assuming they have upgrades available) and log to $PWD\Logs\ChocoLog.txt
function Write-TimeStampOutput {
Param(
[Parameter()]
[String]
$InputObject,
[Parameter(ParameterSetName = 'LongDateTime', Mandatory)]
[switch]
$LongDateTime,
[Parameter(ParameterSetName = 'ShortDateTime', Mandatory)]
[switch]
$ShortDateTime,
[Parameter(ParameterSetName = 'LongDate', Mandatory)]
[switch]
$LongDate,
[Parameter(ParameterSetName = 'LongTime', Mandatory)]
[switch]
$LongTime,
[Parameter(ParameterSetName = 'ShortDate', Mandatory)]
[switch]
$ShortDate,
[Parameter(ParameterSetName = 'ShortTime', Mandatory)]
[switch]
$ShortTime,
[Parameter(ParameterSetName = 'ShortTime')]
[Parameter(ParameterSetName = 'ShortDate')]
[Parameter(ParameterSetName = 'LongTime')]
[Parameter(ParameterSetName = 'LongDate')]
[Parameter(ParameterSetName = 'LongDateTime')]
[Parameter(ParameterSetName = 'ShortDateTime')]
[switch]
$UTC
)
begin {
#$PSBoundParameters.Keys
}
process {
$Now = switch ($PSBoundParameters.Keys) {
'ShortTime' {
[datetime]::Now.ToShortTimeString()
}
'LongTime' {
[datetime]::Now.ToLongTimeString()
}
'ShortDate' {
[datetime]::Now.ToShortDateString()
}
'LongDate' {
[datetime]::Now.ToLongDateString()
}
'LongDateTime' {
[datetime]::Now.ToString('f', [cultureinfo]::CurrentCulture)
}
'ShortDateTime' {
[datetime]::Now.ToString('MM/dd/yyyy|hh:mm:ss')
}
}
if ($UTC) {
$Now = '{0}|UTC{1}' -f $Now, ([datetime]::Now.ToString('%K'))
}
$NowString = '[{0}]' -f $Now
$OutMessage = '{0} {1}' -f $NowString, $InputObject
Write-Output $OutMessage
}
end {
}
}
Write-TimeStampOutput -InputObject 'Getting Locally Installed Chocolatey Packages...' -ShortDateTime -UTC *>> $PWD\Logs\ChocoLog.txt
$ChocoApps = choco list -lo -idonly
$OutMessage = 'Found {0} Chocolatey Packages Installed...' -f $ChocoApps.Count
Write-TimeStampOutput -InputObject $OutMessage -ShortDateTime -UTC *>> $PWD\Logs\ChocoLog.txt
$OutMessage = 'Starting Chocolatey Package Upgrades...'
Write-TimeStampOutput -InputObject $OutMessage -ShortDateTime -UTC *>> $PWD\Logs\ChocoLog.txt
choco upgrade $ChocoApps --y *>> $PWD\Logs\ChocoLog.txt
$OutMessage = 'Finished Chocolatey Package Upgrades...'
Write-TimeStampOutput -InputObject $OutMessage -ShortDateTime -UTC *>> $PWD\Logs\ChocoLog.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment