Hyper-V PowerShell Windows Server 2016 Windows Server 2019 Windows Server 2022

PowerShell: BgInfo deployment script for Windows Server 2016, 2019 and 2022

 [January 16, 2022 – reviewed & updated]

Probably everyone knows the Windows Sysinternals tool BgInfo (currently version 4.28). For those who don’t, it’s a great free tool from Microsoft which captures system information form a workstation or server (probably where it is the most useful) and displays that relevant data directly on the desktop of that particular machine. It can show useful information like, DNS settings, used IP Addresses, computer name, domain name, OS version, memory, service pack version, etc.

Whenever I create a new Windows Server 2016 or 2019, or these days a 2022 Virtual Machine (VM) template for customers, I mostly add this tool in the base image (also called golden image) and set it so it starts up automatically whenever a user logs on to the server. To automate this process, I wrote a PowerShell script which automates the complete BgInfo installation and configuration.

This PowerShell script will do all of the following:

  • Check if the PowerShell window is running as Administrator (which is a requirement), otherwise the PowerShell script will be exited.
  • Create the BgInfo folder on the C: drive if the folder does not already exist.
  • Download the latest BgInfo tool from the Sysinternals webpage.
  • Extract and cleanup the BGInfo.zip file in the BgInfo folder.
  • Download the logon.bgi file which holds the preferred settings.
  • Extract and cleanup the LogonBgi.zip file in the BgInfo folder.
  • Create the registry key (regkey) to AutoStart the BgInfo tool in combination with the logon.bgi config file.
  • Start BgInfo for the first time.
  • Exit the PowerShell window upon completion.


To use the script copy and save it as Deploy-BgInfo-WS2016-WS2019-WS2022.ps1 or download it from GitHub. Leave the variables like they are, or if desired, adjust them to your use. Then run the (customized) script with Administrator privileges from Windows PowerShell from the server you wish to use for your VM template or from any other server you want to use BgInfo on.


Prerequisites

  • OS: Windows Server 2016, 2019 or 2022
  • PowerShell: Windows PowerShell 5.1
  • Other: Internet connectivity


PowerShell Script

<#
.SYNOPSIS

A script used to download, install and configure the latest BgInfo version on a Windows Server 2016, 2019 or 2022.

.DESCRIPTION

A script used to download, install and configure the latest BgInfo version (v4.28) on a Windows Server 2016, 2019 or 2022. 
The BgInfo folder will be created on the C: drive if the folder does not already exist. 
Then the latest BgInfo.zip file will be downloaded and extracted in the BgInfo folder. 
The LogonBgi.zip file which holds the preferred settings will also be downloaded and extracted to the BgInfo folder. 
After extraction both .zip files will be deleted. 
A registry key (regkey) to AutoStart the BgInfo tool in combination with the logon.bgi config file will be created. 
At the end of the script BgInfo will be started for the first time and the PowerShell window will be closed.

.NOTES

File Name:      Deploy-BgInfo-WS2016-WS2019-WS2022.ps1
Created:        08/09/2019
Last modified:  16/01/2022
Author:         Wim Matthyssen
PowerShell:     5.1 or above 
Requires:       -RunAsAdministrator
OS:             Windows Server 2016, Windows Server 2019 and Windows Server 2022
Version:        3.0
Action:         Change variables were needed to fit your needs
Disclaimer:     This script is provided "As Is" with no warranties.

.EXAMPLE

.\Deploy-BgInfo-WS2016-WS2019-WS2022.ps1

.LINK

https://wmatthyssen.com/2019/09/09/powershell-bginfo-automation-script-for-windows-server-2016-2019/
#>

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Variables

$bgInfoFolder = "C:\BgInfo"
$bgInfoFolderContent = $bgInfoFolder + "\*"
$itemType = "Directory"
$bgInfoUrl = "https://download.sysinternals.com/files/BGInfo.zip"
$bgInfoZip = "C:\BgInfo\BGInfo.zip"
$bgInfoEula = "C:\BgInfo\Eula.txt"
$logonBgiUrl = "https://tinyurl.com/yxlxbgun"
$logonBgiZip = "C:\BgInfo\LogonBgi.zip"
$bgInfoRegPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
$bgInfoRegKey = "BgInfo"
$bgInfoRegType = "String"
$bgInfoRegKeyValue = "C:\BgInfo\Bginfo64.exe C:\BgInfo\logon.bgi /timer:0 /nolicprompt"
$regKeyExists = (Get-Item $bgInfoRegPath -EA Ignore).Property -contains $bgInfoRegkey

$global:currenttime= Set-PSBreakpoint -Variable currenttime -Mode Read -Action {$global:currenttime= Get-Date -UFormat "%A %m/%d/%Y %R"}
$foregroundColor1 = "Red"
$foregroundColor2 = "Yellow"
$writeEmptyLine = "`n"
$writeSeperatorSpaces = " - "

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Check if PowerShell is running as Administrator, otherwise exit the script

$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdministrator = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if ($isAdministrator -eq $false) 
{
    Write-Host ($writeEmptyLine + "# Please run PowerShell as Administrator" + $writeSeperatorSpaces + $currentTime)`
    -foregroundcolor $foregroundColor1 $writeEmptyLine
    exit
}
 
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Start script execution

Write-Host ($writeEmptyLine + "# BgInfo deployment script started." + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine 
 
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Create BgInfo folder on C: if it not exists, else delete it's content

If (!(Test-Path -Path $bgInfoFolder))
{
   New-Item -ItemType $itemType -Force -Path $bgInfoFolder
   Write-Host ($writeEmptyLine + "# BgInfo folder created" + $writeSeperatorSpaces + $currentTime)`
   -foregroundcolor $foregroundColor2 $writeEmptyLine
}
Else
{
   Write-Host ($writeEmptyLine + "# BgInfo folder already exists" + $writeSeperatorSpaces + $currentTime)`
   -foregroundcolor $foregroundColor2 $writeEmptyLine
   Remove-Item $bgInfoFolderContent -Force -Recurse -ErrorAction SilentlyContinue
   Write-Host ($writeEmptyLine + "# Content existing BgInfo folder deleted" + $writeSeperatorSpaces + $currentTime)`
   -foregroundcolor $foregroundColor2 $writeEmptyLine
}

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Download, save and extract latest BGInfo software to C:\BgInfo

Import-Module BitsTransfer
Start-BitsTransfer -Source $bgInfoUrl -Destination $bgInfoZip
Expand-Archive -LiteralPath $bgInfoZip -DestinationPath $bgInfoFolder -Force
Remove-Item $bgInfoZip
Remove-Item $bgInfoEula

Write-Host ($writeEmptyLine + "# bginfo.exe available" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Download, save and extract logon.bgi file to C:\BgInfo

Invoke-WebRequest -Uri $logonBgiUrl -OutFile $logonBgiZip
Expand-Archive -LiteralPath $logonBgiZip -DestinationPath $bgInfoFolder -Force
Remove-Item $logonBgiZip

Write-Host ($writeEmptyLine + "# logon.bgi available" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Create BgInfo Registry Key to AutoStart

If ($regKeyExists -eq $True)
{
   Write-Host ($writeEmptyLine + "# BgInfo regkey exists, script wil go on" + $writeSeperatorSpaces + $currentTime)`
   -foregroundcolor $foregroundColor1 $writeEmptyLine
}
Else
{
   New-ItemProperty -Path $bgInfoRegPath -Name $bgInfoRegkey -PropertyType $bgInfoRegType -Value $bgInfoRegkeyValue

   Write-Host ($writeEmptyLine + "# BgInfo regkey added" + $writeSeperatorSpaces + $currentTime)`
   -foregroundcolor $foregroundColor2 $writeEmptyLine
}

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Run BgInfo

C:\BgInfo\Bginfo64.exe C:\BgInfo\logon.bgi /timer:0 /nolicprompt

Write-Host ($writeEmptyLine + "# BgInfo has ran for the first time" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine 

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Exit PowerShell window 3 seconds after completion

Write-Host ($writeEmptyLine + "# Script completed, the PowerShell window will close in 3 seconds" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
Start-Sleep 3 
stop-process -Id $PID 

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------






At the moment when running BgInfo on a Windows Server 2022, it will report OS version “Windows Server 2016”. This will probably be fixed in a new release or update. But in the meantime you can use the following workaround.


Customize logon.bgi

If you want to change any configuration setting (for example the font style or published info), just open the logon.bgi file and adjust the settings to your preferences. Click OK to save and set the new settings.



I hope this PowerShell script comes in handy for you whenever you want to use BgInfo on a server or when you create a VM template.

If you have any questions or recommendations about it, feel free to contact me through my Twitter handle (@wmatthyssen) or to leave a comment.


11 comments on “PowerShell: BgInfo deployment script for Windows Server 2016, 2019 and 2022

  1. Pingback: PowerShell: BgInfo Automation script for Windows Server 2012 R2 – Wim Matthyssen

  2. Pingback: PowerShell: Set customizations for a Windows Server 2016 or 2019 base image – Wim Matthyssen

  3. Awesome Matt, thank you for the script, saved me a ton of time!

    Like

  4. Hi Matt, great script saves a lot of time..thanks, I looking for some condition base background wallpaper, if std windows blue and for cluster role servers a brown in color..is this can be achieved..?

    Like

  5. Hey Wim,

    How would you tackle the way varonis exploits BGINFO?
    I would but the BGI file on a “protected” location of the OS, but not sure if thats sufficiant.
    https://www.varonis.com/blog/exploiting-bginfo-to-infiltrate-a-corporate-network/

    Like

    • Hi Kris, I only use the script on servers, so I don’t directly see the problem (except maybe on RDS servers). I suppose you want to use it on a Windows 10 device. If so, you can block the .bgi extension via Exchange (but it depends which license your using off course, to be able to also block it in a .ZIP file), otherwise block it in your antivirus or you could use application whitelisting using Software Restriction Policies for all users except local administrators.

      Like

  6. It did not run on 2019 servers. I opened with Admin Powershell.

    S C:\Temp> .\Deploy-BgInfo-WS2016-WS2019-WS2022.ps1
    At C:\Temp\Deploy-BgInfo-WS2016-WS2019-WS2022.ps1:200 char:21
    + Sign up
    + ~
    The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double
    quotation marks (“&”) to pass it as part of a string.
    At C:\Temp\Deploy-BgInfo-WS2016-WS2019-WS2022.ps1:227 char:189
    + … ata-ga-click=”(Logged out) Header, go to Features”>Features <span cla …
    + ~
    The '→Mobile <span cla …
    + ~
    The '→Actions <span cla …
    + ~
    The '→Codespaces <span cla …
    + ~
    The '→Packages <span cla …
    + ~
    The '<' operator is reserved for future use.
    Not all parse errors were reported. Correct the reported errors and try again.
    + CategoryInfo : ParserError: (:) [], ParseExce

    Like

    • Hello Arindam,

      It runs without any problems. How did you used the script. Did you copied it from the website or do you use the GitHub source? If so, I think you have copied some wrong characters on the lines specified. Could you validate?

      Like

  7. Pingback: BgInfo 4.28 workaround: Use a WMI query to report Windows Server 2022 correctly as the OS Version – Wim Matthyssen

  8. Pingback: BgInfo 4.28 workaround: Use a WMI query to report Windows Server 2022 correctly as the OS Version – Stack IT News

  9. Great Script!Is it okay for you if i put it in the Tactical RMM Script Repository with some changes?
    Greetings, Steven

    Like

Leave a Reply to wmatthyssen Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: