Watch Demo×
×

See NinjaOne in action!

By submitting this form, I accept NinjaOne's privacy policy.

Streamline System Management: BGInfo PowerShell Deployment Script

Key takeaways

  • Automated BGInfo deployment: The script provides a seamless, automated method to deploy BGInfo across multiple Windows systems.
  • Efficiency for IT professionals: Saves time and reduces manual effort for IT professionals and MSPs managing system information displays.
  • Customizable configuration: Allows customization through a configurable parameter to suit different environments and requirements.
  • Administrative privileges required: The script needs to run with administrator rights to access system directories and modify settings.
  • Error handling and robustness: Includes comprehensive error handling, ensuring reliability and clear communication during execution.
  • Security awareness: Important to be aware of the information displayed by BGInfo to avoid exposing sensitive data.
  • Scalability and consistency: Ensures uniformity in system information display across all machines in an organization.
  • Compatibility with modern windows systems: Primarily supports Windows 10 and Windows Server 2016 onwards.
  • Complementary to comprehensive IT tools: Works well in conjunction with broader IT management platforms like NinjaOne for enhanced visibility and control.

In the ever-evolving landscape of information technology, efficient management and configuration of IT systems are crucial. BGInfo, a renowned tool in the Windows environment, plays a vital role in this regard. This tool displays essential system information on the desktop background, proving invaluable for IT professionals and Managed Service Providers (MSPs). Today, we explore a PowerShell script designed for the seamless installation and configuration of BGInfo across Windows systems.

Background

BGInfo, part of the Sysinternals Suite, has been a staple in IT professionals’ toolkits for years. It displays system information like IP address, computer name, and operating system version on the desktop, providing quick insights into system configurations. In a world where swift information access and system monitoring are key, automating the deployment of BGInfo using a PowerShell script is both a time-saver and an efficiency booster, especially for IT professionals and MSPs managing multiple machines.

The script:

#Requires -Version 2.0

<#
.SYNOPSIS
    Downloads, installs and sets up BGInfo to run for all users.
.DESCRIPTION
    Downloads, installs and sets up BGInfo to run for all users.
    Uses the default configuration if no .bgi file path or URL is specified.

    Note: Users that are already logged in will need to logout and login to have BGInfo update their desktop background.

.EXAMPLE
    (No Parameters)
    ## EXAMPLE OUTPUT WITHOUT PARAMS ##
    Create Directory: C:\WINDOWS\System32\SysInternals
    Downloading https://live.sysinternals.com/Bginfo.exe
    Created Shortcut: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\BGInfo.lnk

.EXAMPLE
    -Config C:\BGInfo\config.bgi
    Specifies the BGInfo configuration file to use.

PARAMETER: -Config C:\BGInfo\config.bgi
    ## EXAMPLE OUTPUT WITHOUT PARAMS ##
    Create Directory: C:\WINDOWS\System32\SysInternals
    Downloading https://live.sysinternals.com/Bginfo.exe
    Created Shortcut: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\BGInfo.lnk
.OUTPUTS
    None
.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2016
    Release Notes: Renamed script and added Script Variable support
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
    Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#>

[CmdletBinding()]
param (
    [Parameter()]
    [string]$Config
)

begin {
    if ($env:bginfoConfigFilePath -and $env:bginfoConfigFilePath -notlike "null") { $Config = $env:bginfoConfigFilePath }
    function Test-IsElevated {
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $p = New-Object System.Security.Principal.WindowsPrincipal($id)
        $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
    }

    function New-Shortcut {
        [CmdletBinding()]
        param(
            [Parameter()]
            [String]$Arguments,
            [Parameter()]
            [String]$IconPath,
            [Parameter(ValueFromPipeline = $True)]
            [String]$Path,
            [Parameter()]
            [String]$Target,
            [Parameter()]
            [String]$WorkingDir
        )
        process {
            Write-Host "Creating Shortcut at $Path"
            $ShellObject = New-Object -ComObject ("WScript.Shell")
            $Shortcut = $ShellObject.CreateShortcut($Path)
            $Shortcut.TargetPath = $Target
            if ($WorkingDir) { $Shortcut.WorkingDirectory = $WorkingDir }
            if ($Arguments) { $ShortCut.Arguments = $Arguments }
            if ($IconPath) { $Shortcut.IconLocation = $IconPath }
            $Shortcut.Save()

            if (!(Test-Path $Path -ErrorAction SilentlyContinue)) {
                Write-Error "Unable to create Shortcut at $Path"
                exit 1
            }
        }
    }
    # Utility function for downloading files.
    function Invoke-Download {
        param(
            [Parameter()]
            [String]$URL,
            [Parameter()]
            [String]$Path,
            [Parameter()]
            [int]$Attempts = 3,
            [Parameter()]
            [Switch]$SkipSleep
        )
        Write-Host "URL given, Downloading the file..."

        $SupportedTLSversions = [enum]::GetValues('Net.SecurityProtocolType')
        if ( ($SupportedTLSversions -contains 'Tls13') -and ($SupportedTLSversions -contains 'Tls12') ) {
            [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol::Tls13 -bor [System.Net.SecurityProtocolType]::Tls12
        }
        elseif ( $SupportedTLSversions -contains 'Tls12' ) {
            [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
        }
        else {
            # Not everything requires TLS 1.2, but we'll try anyway.
            Write-Warning "TLS 1.2 and or TLS 1.3 are not supported on this system. This download may fail!"
            if ($PSVersionTable.PSVersion.Major -lt 3) {
                Write-Warning "PowerShell 2 / .NET 2.0 doesn't support TLS 1.2."
            }
        }

        $i = 1
        While ($i -le $Attempts) {
            # Some cloud services have rate-limiting
            if (-not ($SkipSleep)) {
                $SleepTime = Get-Random -Minimum 3 -Maximum 15
                Write-Host "Waiting for $SleepTime seconds."
                Start-Sleep -Seconds $SleepTime
            }
        
            if ($i -ne 1) { Write-Host "" }
            Write-Host "Download Attempt $i"

            try {
                # Invoke-WebRequest is preferred because it supports links that redirect, e.g., https://t.ly
                if ($PSVersionTable.PSVersion.Major -lt 4) {
                    # Downloads the file
                    $WebClient = New-Object System.Net.WebClient
                    $WebClient.DownloadFile($URL, $Path)
                }
                else {
                    # Standard options
                    $WebRequestArgs = @{
                        Uri                = $URL
                        OutFile            = $Path
                        MaximumRedirection = 10
                        UseBasicParsing    = $true
                    }

                    # Downloads the file
                    Invoke-WebRequest @WebRequestArgs
                }

                $File = Test-Path -Path $Path -ErrorAction SilentlyContinue
            }
            catch {
                Write-Warning "An error has occurred while downloading!"
                Write-Warning $_.Exception.Message

                if (Test-Path -Path $Path -ErrorAction SilentlyContinue) {
                    Remove-Item $Path -Force -Confirm:$false -ErrorAction SilentlyContinue
                }

                $File = $False
            }

            if ($File) {
                $i = $Attempts
            }
            else {
                Write-Warning "File failed to download."
                Write-Host ""
            }

            $i++
        }

        if (-not (Test-Path $Path)) {
            throw "Failed to download file!"
        }
        else {
            Write-Host "Download Successful!"
        }
    }

    function Install-SysInternalsTool {
        [CmdletBinding()]
        param()
        # Target directory is %WinDir%C:\Windows\System32\SysInternals
        $TargetDir = Join-Path -Path $env:WinDir -ChildPath "System32\SysInternals"

        # Tools to be downloaded
        $Tools = @(
            [PSCustomObject]@{
                Name     = "Bginfo"
                FileName = "Bginfo.exe"
                URL      = "https://live.sysinternals.com/Bginfo.exe"
            }
        )

        # Create Directory
        if (-not $(Test-Path $TargetDir -ErrorAction SilentlyContinue)) {
            Write-Host "Create Directory: $TargetDir"
            New-Item -ItemType Directory -Path $TargetDir -Force -ErrorAction SilentlyContinue
        }

        # Download tools to target directory
        try {
            foreach ($Tool in $Tools) {
                $FilePath = Join-Path $TargetDir $Tool.FileName
                Write-Host "Downloading $($Tool.Name) to $FilePath"
                Invoke-Download -URL $Tool.URL -Path $FilePath
            }
        }
        catch {
            throw $_
        }
    }
    function Register-BGInfoStartup {
        [CmdletBinding()]
        param(
            [Parameter()][string]$Config
        )
        $ExePath = Join-Path -Path $env:WinDir -ChildPath "System32\SysInternals\BGInfo.exe"
        if (-not $(Test-Path -Path $ExePath -ErrorAction SilentlyContinue)) {
            throw "BGInfo.exe is not found at $ExePath"
        }

        # Register Startup command for All User
        try {
            $StartupPath = Join-Path -Path $env:ProgramData -ChildPath "Microsoft\Windows\Start Menu\Programs\StartUp\StartupBGInfo.lnk"
            
            if ($(Test-Path -Path $StartupPath -ErrorAction SilentlyContinue)) {
                Remove-Item -Path $StartupPath -ErrorAction SilentlyContinue
            }
            if ($Config -and $(Test-Path -Path $Config -ErrorAction SilentlyContinue)) {
                New-Shortcut -Path $StartupPath -Arguments "/iq `"$Config`" /accepteula /timer:0 /silent" -Target $ExePath
            }
            else {
                New-Shortcut -Path $StartupPath -Arguments "/accepteula /timer:0 /silent" -Target $ExePath
            }

            Write-Host "Created Startup: $StartupPath"
        }
        catch {
            throw "Unable to create shortcut for BGInfo.exe"
        }
    }
}
process {
    if (-not (Test-IsElevated)) {
        Write-Error -Message "Access Denied. Please run with Administrator privileges."
        exit 1
    }

    try {
        Install-SysInternalsTool

        if ($Config) {
            if (-not $(Test-Path -Path $Config -ErrorAction SilentlyContinue)) {
                try {
                    if (-not (Test-Path -Path "$Env:PROGRAMDATA\SysInternals" -ErrorAction SilentlyContinue)) {
                        New-Item -ItemType Directory -Path "$Env:PROGRAMDATA\SysInternals" -Force
                    }
                    Invoke-Download -URL $Config -Path $(Join-Path -Path $env:PROGRAMDATA -ChildPath "SysInternals\bginfoConfig.bgi")
                    $Config = $(Join-Path -Path $env:PROGRAMDATA -ChildPath "SysInternals\bginfoConfig.bgi")
                }
                catch {
                    Write-Error "Failed to download from provided Url or that the Path to the specified file does not exist."
                    Write-Error $_
                    exit 1
                }
            }
            Register-BGInfoStartup -Config $Config
        }
        else {
            Register-BGInfoStartup
        }
    }
    catch {
        Write-Error $_
        exit 1
    }

    Write-Host "Successfully installed and set up bginfo. Bginfo will start the next time the end user logs in or restarts."
    exit 0
}
end {
    
    
    
}

 

Access over 300+ scripts in the NinjaOne Dojo

Get Access

Detailed breakdown

The script operates in several stages:

  • Parameter configuration: It begins by allowing the user to specify a configuration file through the $Config parameter. If a specific configuration file path is not given, it defaults to a standard setup.
  • Elevation check: The script checks if it’s running with administrator privileges, which is essential for accessing system directories and modifying startup settings.
  • Downloading and installing BGInfo: The script creates a directory at C:\WINDOWS\System32\SysInternals and downloads BGInfo from the official Sysinternals live site. It handles potential download issues, such as TLS incompatibility and retry logic.
  • Setting up startup shortcut: The script creates a shortcut in the common startup folder. This ensures BGInfo runs for all users at login. It can use a specified configuration file or default settings.
  • Error handling: Throughout, the script includes robust error handling, ensuring any issues are clearly communicated to the user.

Potential use cases

Consider a scenario where an MSP needs to deploy BGInfo across a fleet of Windows machines in a corporate environment. This script automates the process, reducing manual intervention and ensuring uniformity in BGInfo configuration across all systems.

Comparisons

Traditionally, deploying BGInfo would involve manual installation and configuration on each machine. This script not only saves time but also reduces the likelihood of human error. It’s a more scalable and reliable approach compared to manual deployment.

FAQs

  • Can this script be customized for different configurations?
    Yes, by changing the $Config parameter.
  • Does this script work on all Windows versions?
    It supports Windows 10 and Windows Server 2016 onwards.

Implications

While the script streamlines deployment, it’s important to be aware of the security implications. Ensuring that the BGInfo configuration doesn’t inadvertently expose sensitive information on the desktop background is vital.

Recommendations

  • Always test the script in a controlled environment before wide-scale deployment.
  • Regularly update the script to accommodate new Windows updates and Sysinternals versions.
  • Be cautious about the information displayed by BGInfo, considering the security context of your environment.

Final thoughts

In the context of system management and monitoring, tools like NinjaOne can complement scripts like this by providing a comprehensive platform for IT management. NinjaOne offers a range of features that synergize with the insights provided by BGInfo, enhancing overall IT infrastructure visibility and control.

Next Steps

Building an efficient and effective IT team requires a centralized solution that acts as your core service deliver tool. NinjaOne enables IT teams to monitor, manage, secure, and support all their devices, wherever they are, without the need for complex on-premises infrastructure.

Learn more about NinjaOne Remote Script Deployment, check out a live tour, or start your free trial of the NinjaOne platform.

Categories:

You might also like

NinjaOne Terms & Conditions

By clicking the “I Accept” button below, you indicate your acceptance of the following legal terms as well as our Terms of Use:

  • Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
  • Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
  • Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library belonging to or under the control of any other software provider.
  • Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
  • Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
  • Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
  • EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).