Skip to content

Instantly share code, notes, and snippets.

@meepak
Created September 5, 2024 00:02
Show Gist options
  • Save meepak/61fe7217d7c6a8488c9fb234bd65de63 to your computer and use it in GitHub Desktop.
Save meepak/61fe7217d7c6a8488c9fb234bd65de63 to your computer and use it in GitHub Desktop.
PowerShell script to add a custom context menu item to windows Desktop, that uses BgInfo.exe from SysInternals to display PC info on desktop.
<#/
.SYNOPSIS
PowerShell script to add a custom context menu item to windows Desktop,
that uses BgInfo.exe from sysinternals to display PC info on desktop.
.DESCRIPTION
This script sets up BGInfo by downloading it if necessary, creating a batch file to run it,
and adding a context menu item to the desktop background for easy access.
- dpk, 09/2024
#>
# Combined BGInfo Setup Script
Write-Host "This script adds a custom context menu item to Windows Desktop that uses BgInfo.exe from sysinternals to display PC info on desktop."
# Define the path for the hidden .bginfo directory in the user's home directory
$BGInfoDir = Join-Path -Path $env:USERPROFILE -ChildPath ".bginfo"
$BatchFilePath = Join-Path -Path $BGInfoDir -ChildPath "update_bginfo.bat"
$regPath = "HKCU:\Software\Classes\DesktopBackground\Shell\UpdateSystemInfo"
#
#*
#**
#**************************
# Setup Function
#**************************
function Setup {
# 1. Ask user to confirm or provide BGInfo executable path
if (-not $BGInfoPath -or -not (Test-Path $BGInfoPath)) {
Write-Host "No BGInfo executable path was provided or found."
$BGInfoPath = Read-Host "Please provide the path to BGInfo executable or leave empty to download automatically"
if (-not $BGInfoPath) {
Write-Host "No path provided. BGInfo will be downloaded from the internet."
}
} else {
Write-Host "BGInfo executable path is already set to: $BGInfoPath"
$confirm = Read-Host "Do you want to use this path? (y/Y to confirm, any other key to change)"
if ($confirm -ne "y" -and $confirm -ne "Y") {
$BGInfoPath = Read-Host "Please provide the new path to BGInfo executable or leave empty to download automatically"
if (-not $BGInfoPath) {
Write-Host "No path provided. BGInfo will be downloaded from the internet."
}
}
}
# 2. Ask user to confirm or provide BGInfo configuration file path
if (-not $BGInfoConfigPath -or -not (Test-Path $BGInfoConfigPath)) {
Write-Host "No BGInfo configuration file path was provided."
$BGInfoConfigPath = Read-Host "Please provide the path to a custom BGInfo configuration file, or leave empty to use the default settings"
if (-not $BGInfoConfigPath) {
Write-Host "No configuration file provided. Default BGInfo settings will be used."
}
} else {
Write-Host "BGInfo configuration path is already set to: $BGInfoConfigPath"
$confirmConfig = Read-Host "Do you want to use this configuration file? (y/Y to confirm, any other key to change)"
if ($confirmConfig -ne "y" -and $confirmConfig -ne "Y") {
$BGInfoConfigPath = Read-Host "Please provide the new path to a custom BGInfo configuration file, or leave empty to use default settings"
if (-not $BGInfoConfigPath) {
Write-Host "No configuration file provided. Default BGInfo settings will be used."
}
}
}
# 3. Create a hidden .bginfo directory for the batch file
if (-not (Test-Path $BGInfoDir)) {
$null = New-Item -Path $BGInfoDir -ItemType Directory
$attr = [System.IO.FileAttributes]::Hidden
[System.IO.File]::SetAttributes($BGInfoDir, $attr)
}
# 4. Download BGInfo if path is not provided
if (-not $BGInfoPath -or -not (Test-Path $BGInfoPath)) {
$TempPath = Join-Path -Path $env:TEMP -ChildPath "BGInfo.zip"
$DownloadUrl = "https://download.sysinternals.com/files/BGInfo.zip"
Invoke-WebRequest -Uri $DownloadUrl -OutFile $TempPath
$ExtractPath = Join-Path -Path $env:TEMP -ChildPath "BGInfoExtract"
Expand-Archive -Path $TempPath -DestinationPath $ExtractPath -Force
$ExtractedExePath = Join-Path -Path $ExtractPath -ChildPath "Bginfo64.exe"
$BGInfoPath = Join-Path -Path $BGInfoDir -ChildPath "Bginfo64.exe"
Copy-Item -Path $ExtractedExePath -Destination $BGInfoPath -Force
Remove-Item -Path $TempPath -Force
Remove-Item -Path $ExtractPath -Recurse -Force
}
# 5. Create the batch file to run BGInfo
$BatchFileContent = "@echo off`n"
if ($BGInfoConfigPath -and (Test-Path $BGInfoConfigPath)) {
$BatchFileContent += "`"$BGInfoPath`" `"$BGInfoConfigPath`" /timer:0 /nolicprompt /silent"
} else {
$BatchFileContent += "`"$BGInfoPath`" /timer:0 /nolicprompt /silent"
}
Set-Content -Path $BatchFilePath -Value $BatchFileContent
# 6. Add registry entries for context menu
$commandRegPath = "$regPath\command"
if (-not (Test-Path $regPath)) {
$null = New-Item -Path $regPath -Force
New-ItemProperty -Path $regPath -Name "MUIVerb" -Value "Update System Info" -PropertyType String -Force | Out-Null
New-ItemProperty -Path $regPath -Name "Icon" -Value $BGInfoPath -PropertyType String -Force | Out-Null
}
if (-not (Test-Path $commandRegPath)) {
$null = New-Item -Path $commandRegPath -Force
New-ItemProperty -Path $commandRegPath -Name "(Default)" -Value "`"$BatchFilePath`"" -PropertyType String -Force | Out-Null
}
Write-Host "Context menu item created successfully. BGInfo setup complete."
}
# End of Setup function
#
#*
#**
#**************************
# Uninstall Function
#**************************
function Uninstall {
Write-Host "Removing changes made by the BGInfo setup script."
# 1. Define the path for the hidden .bginfo directory in the user's home directory
$BGInfoDir = Join-Path -Path $env:USERPROFILE -ChildPath ".bginfo"
$BatchFilePath = Join-Path -Path $BGInfoDir -ChildPath "update_bginfo.bat"
$BGInfoPath = Join-Path -Path $BGInfoDir -ChildPath "Bginfo64.exe"
$BGInfoConfigPath = Join-Path -Path $BGInfoDir -ChildPath "BginfoConfig.bgi" # Assuming the config file is named like this
$regPath = "HKCU:\Software\Classes\DesktopBackground\Shell\UpdateSystemInfo"
# 2. Remove the registry entries
if (Test-Path $regPath) {
Remove-Item -Path $regPath -Recurse -Force
Write-Host "Registry entries removed successfully."
} else {
Write-Host "No registry entries found to remove."
}
# 3. Remove the batch file if it exists
if (Test-Path $BatchFilePath) {
Remove-Item -Path $BatchFilePath -Force
Write-Host "Batch file removed successfully."
} else {
Write-Host "No batch file found to remove."
}
# 4. Ask user if they want to remove the BGInfo executable and configuration file
$removeBGInfo = Read-Host "Do you want to remove the BGInfo executable and configuration file? (y/Y to remove, any other key to leave them)"
if ($removeBGInfo -eq "y" -or $removeBGInfo -eq "Y") {
# Remove BGInfo executable if it exists
if (Test-Path $BGInfoPath) {
Remove-Item -Path $BGInfoPath -Force
Write-Host "BGInfo executable removed successfully."
} else {
Write-Host "No BGInfo executable found to remove."
}
# Remove BGInfo configuration file if it exists
if (Test-Path $BGInfoConfigPath) {
Remove-Item -Path $BGInfoConfigPath -Force
Write-Host "BGInfo configuration file removed successfully."
} else {
Write-Host "No BGInfo configuration file found to remove."
}
} else {
Write-Host "BGInfo executable and configuration file left as is."
}
# 5. Remove the .bginfo directory if it is empty
if (Test-Path $BGInfoDir) {
if ((Get-ChildItem -Path $BGInfoDir | Measure-Object).Count -eq 0) {
Remove-Item -Path $BGInfoDir -Force
Write-Host ".bginfo directory removed successfully."
} else {
Write-Host ".bginfo directory is not empty. Manual review recommended."
}
} else {
# Display full path of .bginfo for user to review
Write-Host "No .bginfo directory found to remove. Path: $BGInfoDir"
}
}
# End of Uninstall function
#******************************************
# Check for previous installation and do fresh installation
$BGInfoExists = Test-Path $BGInfoDir
$RegPathExists = Test-Path $regPath
if ($BGInfoExists -or $RegPathExists) {
Write-Host "Previous installation of this script detected."
$userChoice = Read-Host "How do you want to proceed? (press u/U to uninstall, default: r/R for reinstall)"
if ($userChoice -eq "u" -or $userChoice -eq "U") {
Uninstall
exit
}
}
$userInstallChoice = Read-Host "Please press y/Y to install? (default: any other key to exit)"
if ($userInstallChoice -ne "y" -and $userInstallChoice -ne "Y") {
Write-Host "Exiting script."
exit
}
Setup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment