[小技巧] Windows Winget Regular Export to specific folder ($scriptfolder\$machine_name)

date
Mar 31, 2023
slug
tip-winget-export-folder-backup
status
Published
summary
小技巧
tags
tip
type
Post
URL

Problem & Summary

  • make sure there is no space in your account user name so that the command path doesn’t need to be escaped for spaces
  • write a script
  • psn_winget_export.ps1
$machineName = $env:COMPUTERNAME -replace ' ', '_'
$date = Get-Date -Format "yyyyMMdd"
$currentTime = Get-Date
$seconds = [math]::truncate($currentTime.TimeOfDay.TotalSeconds)

$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$folderPath = Join-Path $scriptPath $machineName

if (-not (Test-Path -Path $folderPath)) {
    New-Item -ItemType Directory -Path $folderPath | Out-Null
}

$filename = "installed_packages_{0}_{1}_{2}.json" -f $machineName, $date, $seconds
$fullPath = Join-Path $folderPath $filename

winget export -o $fullPath
notion image

在任何位置测试手动测试 -

powershell.exe -ExecutionPolicy Bypass -File C:\Users\LAVA LAKE\SynologyDrive\__QH_PSN\_PSN_Config\__bak\psn_winget_export.ps1
 

使用taskschd.msc 来建立类似Cron Job

notion image
notion image
 
notion image

Screenshots

 

© Ying Bun 2021 - 2024