Last active
April 30, 2025 23:29
-
-
Save hudsonm62/79fcc52066c190c4acb258df4780bcc4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Downloads or Updates the latest version of NuGet & 'Get-WindowsAutopilotInfo' script from the PowerShell Gallery, then runs it to retrieve Windows Autopilot information. | |
.PARAMETER OutFolder | |
Specifies the folder where the output CSV file will be saved. Default: `C:\Temp` | |
.PARAMETER FileName | |
Specifies the name of the output CSV file. Default: `AutopilotHWID.csv` | |
.PARAMETER PassThru | |
If specified, the script will output the Autopilot information as a PowerShell Object instead of writing to a file. | |
.EXAMPLE | |
.\Get-WindowsAutopilotInfo.ps1 -OutFolder "C:\Reports" -FileName "MyAutopilotInfo.csv" | |
This command will save the Autopilot information to `C:\Reports\MyAutopilotInfo.csv`. | |
.EXAMPLE | |
.\Get-WindowsAutopilotInfo.ps1 -PassThru | |
This command will return the Autopilot information as a PowerShell object for further use in the pipeline. | |
.NOTES | |
- The script modifies the execution policy for the current session to allow for the execution of scripts. | |
- It temporarily adds the `C:\Program Files\WindowsPowerShell\Scripts` directory to the PATH environment variable. | |
.INPUTS | |
This script does not accept any input via the pipeline. | |
.OUTPUTS | |
- If `OutFolder` and `FileName` are specified, a CSV file containing Autopilot information is created. | |
- If `PassThru` is specified, a PowerShell object representing the Autopilot information is returned. | |
.LINK | |
https://www.powershellgallery.com/packages/Get-WindowsAutoPilotInfo | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(ParameterSetName = "Out", Mandatory = $false)] | |
[string]$OutFolder = $env:SystemDrive, | |
[Parameter(ParameterSetName = "Out", Mandatory = $false)] | |
[string]$FileName = "AutopilotHWID.csv", | |
# Outputs Information as PS Object, instead of writing to a file. | |
[Parameter(ParameterSetName = "Pass", Mandatory = $true)] | |
[switch]$PassThru = $false | |
) | |
begin { | |
Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned -Force | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | |
if ($PSCmdlet.ParameterSetName -eq "Out") { | |
# Folder Structure | |
if (!(Test-Path $OutFolder)) { | |
New-Item -Type Directory -Path $OutFolder -Force | Out-Null | |
} | |
} | |
} | |
process { | |
# Install/Update NuGet & Get-WindowsAutopilotInfo | |
Get-PackageProvider -Name Nuget -ForceBootstrap | Out-Null | |
Install-Script -Name Get-WindowsAutopilotInfo -Force -ErrorAction Stop | |
# Add \scripts to PATH - Temporary only for this session. | |
$env:Path += ";C:\Program Files\WindowsPowerShell\Scripts" | |
# Output Info to File | |
if ($PSCmdlet.ParameterSetName -eq "Out") { | |
$OutFile = Join-Path $OutFolder $FileName | |
Get-WindowsAutopilotInfo -OutputFile $OutFile | |
if (Test-Path $OutFile) { | |
Write-Output "AutoPilot Info saved to '$OutFile'." | |
exit 0 | |
} else { | |
throw "Failed to find '$OutFile' after script execution.. This may be due to a permissions error." | |
} | |
} | |
# PassThru as PSObject | |
return Get-WindowsAutopilotInfo | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment