r/PowerShell • u/NavyWolf23 • Dec 12 '25
Needing help getting a powershell script to read the output of another command.
My main goal with this script is to execute an application provided by a colleague that reads the Windows Edition from the MSDM table in BIOS, have the PowerShell use some like query to read if the output of that is Home or Pro (because the output is 4 lines long with other information) and save it in a task sequence variable (MDT to be specific if SCCM environment object works differently.) I am still learning PowerShell and I am using AI to assist so sorry if the error is obvious but here is the code for my script:
# --- 1. Setup ---
# Define the specific folder where the EXE and its dependencies are located
$targetFolder = "Z:\Scripts\CustomAssets\EnumProductKey"
$exeName = "EnumProductKey.exe"
# --- 2. Load TS Bridge ---
try {
$tsenv = New-Object -ComObject Microsoft.SMS.TSEnvironment
}
catch {
Write-Error "CRITICAL: Could not load the Task Sequence Environment object."
exit 1
}
# --- 3. Change Directory and Execute ---
# Save the current location to return to it later (good practice)
Push-Location -Path $targetFolder
# Execute using relative path (.\) so we are strictly running "from" the folder
# We use try/catch here in case the EXE is missing or crashes
try {
# The '.\' forces PowerShell to look in the current folder ($targetFolder)
$exeOutput = & ".\$exeName"
}
catch {
Write-Warning "Failed to execute $exeName in $targetFolder"
}
# --- 4. Process Output ---
$edition = ""
if ($exeOutput) {
foreach ($line in $exeOutput) {
$lowerLine = $line.ToLower()
if ($lowerLine -like '*home*') { $edition = "Home"; break }
elseif ($lowerLine -like '*pro*') { $edition = "Pro"; break }
}
}
# --- 5. Cleanup and Save ---
# Return to the original directory
Pop-Location
# Save variable
$tsenv.Value("Edition") = $edition
Write-Host "Edition to: $edition"
10
u/BlackV Dec 12 '25
if the dirty exe
EnumProductKey.exeis not properly outputting to the console, then you'll have to redirect the output streamhttps://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-7.5
Or find out that the dirty exe is doing and just do that natively in powershell
as a guess
3
u/purplemonkeymad Dec 13 '25
Could you not just load the hive then read it from the registry ie:
That's probably what the program is doing anyway.
Will also get you some more info about the windows install.
4
u/realslacker Dec 13 '25
Redirect the streams:
1
u/HardyPotato Dec 12 '25
what is not working exactly ?
1
u/Last_Technology_1461 Dec 13 '25
is there anything in $exeOutput = & ".\$exeName" ? For trail probably create a log file and throw everything which could be a problem in log file.
1
u/narcissisadmin Dec 13 '25
If you indent your code (4 spaces at the start of each line) then it will appear nicely here.