Solved powershell variable multiple output choice


shoober420

Well-known member
Member
VIP
Local time
12:38 PM
Posts
231
OS
Windows 11 27965
i tried googling how to do this, but the results think im asking something else. its hard to word what im trying to do in a search line. theres a string expression in this script that can output two monitors if they are both hooked up.

Code:
$DisplayPath = "EDID_{0}_{1}_{2}_{3}" -f $Manufacturer, $ProductID, $Model, $DisplayPathDigit

is there any way to allow a choice selection for a variable that can have multiple output?
 

My Computer My Computer

At a glance

Windows 11 27965Intel i7 7700 @4.0ghz64gb DDR4Radeon RX 5500 XT
OS
Windows 11 27965
Computer type
PC/Desktop
CPU
Intel i7 7700 @4.0ghz
Memory
64gb DDR4
Graphics Card(s)
Radeon RX 5500 XT
Sound Card
Topping D50s
Hard Drives
NVMe
PSU
Corsair
Keyboard
Stelseries G6v2
Mouse
Zowie EC2
Other Info
https://www.youtube.com/shoober420
There's basically nothing to go on here. How about some context?
 

My Computer My Computer

At a glance

Windows 11 Pro 25H212th Gen Core i7-1260P64 GB Micron PC4-25600Intel Iris Xe Graphics
OS
Windows 11 Pro 25H2
Computer type
PC/Desktop
Manufacturer/Model
Intel NUC12WSHi7
CPU
12th Gen Core i7-1260P
Motherboard
NUC12WSBi7
Memory
64 GB Micron PC4-25600
Graphics Card(s)
Intel Iris Xe Graphics
Sound Card
on-board Realtek HD Audio
Monitor(s) Displays
Dell U3219Q
Screen Resolution
3840 x 2160
Hard Drives
Samsung SSD 990 PRO 1TB
Crucial MX500 2 TB
Antivirus
Microsoft Defender
Something like this;

Powershell:
$monitors = @(
    @{ Manufacturer = "24a"; ProductID = "35236dfg"; Model = "ModelADDFS"; DisplayPathDigit = "00" },
    @{ Manufacturer = "dsfg"; ProductID = "asdfas"; Model = "ModelSDF"; DisplayPathDigit = "01" }
)

$DisplayPaths = @()
foreach ($monitor in $monitors) {
    $DisplayPath = "EDID_{0}_{1}_{2}_{3}" -f $monitor.Manufacturer, $monitor.ProductID, $monitor.Model, $monitor.DisplayPathDigit
    $DisplayPaths += $DisplayPath
}
for ($i = 0; $i -lt $DisplayPaths.Count; $i++) {
    Write-Host "$($i + 1): $($DisplayPaths[$i])"
}
$choice = Read-Host "Please select a display by number"
if ($choice -as [int] -and $choice -gt 0 -and $choice -le $DisplayPaths.Count) {
    $selectedDisplayPath = $DisplayPaths[$choice - 1]
    Write-Host "You selected: $selectedDisplayPath"
} else {
    Write-Host "Invalid selection. Please run the program again and choose a valid number."
}

PS C:\Users\User\Desktop> .\test.ps1
1: EDID_24a_35236dfg_ModelADDFS_00
2: EDID_dsfg_asdfas_ModelSDF_01
Please select a display path by number:
 

My Computer My Computer

At a glance

Windows 11
OS
Windows 11
Computer type
PC/Desktop
I dunno, the average user has no clue what all that gibberish means. They would expect "Manufacturer / Model / [Display # or SN #]" when selecting an option.

Code:
$Monitor_List = @(
    Get-WmiObject WmiMonitorID -Namespace root\wmi | foreach {
        [PSCustomObject]@{
            Instance = $_.InstanceName
            Manufacturer = [System.Text.Encoding]::ASCII.GetString($_.UserFriendlyName).Replace("$([char]0x00)","")
            Serial = [System.Text.Encoding]::ASCII.GetString($_.SerialNumberID).Replace("$([char]0x00)","")
        }
    }
)

$index = 1
foreach ($Monitor in $Monitor_List) {
    '{0}. {1} - S/N: {2}' -f $index++, $Monitor.Manufacturer, $Monitor.Serial
}
Write-Host ''

while ($true) {
    $choice = Read-Host 'Please select a display by number'
    if ([int]$choice -in 1..$Monitor_List.Count) {
        break
    }

    Write-Host 'Invalid selection.'
}

$selectedDisplayPath = $Monitor_List[$choice - 1].Instance
Write-Host "You selected: $selectedDisplayPath"
 

My Computer My Computer

At a glance

Windows 7
OS
Windows 7
it works perfectly good sir, i couldnt appreciate you more
 

My Computer My Computer

At a glance

Windows 11 27965Intel i7 7700 @4.0ghz64gb DDR4Radeon RX 5500 XT
OS
Windows 11 27965
Computer type
PC/Desktop
CPU
Intel i7 7700 @4.0ghz
Memory
64gb DDR4
Graphics Card(s)
Radeon RX 5500 XT
Sound Card
Topping D50s
Hard Drives
NVMe
PSU
Corsair
Keyboard
Stelseries G6v2
Mouse
Zowie EC2
Other Info
https://www.youtube.com/shoober420
@garlin
it looks like i need to adjust something else for it to select the right monitor. i came to the conclusion that i need to make the new $Monitor_List replace the old method. i came up with this so far.

Code:
$Monitor_List = @(
    Get-WmiObject WmiMonitorID -Namespace root\wmi | foreach {
        [PSCustomObject]@{
            Instance = $_.InstanceName

            bigEndianInt32 = [Convert]::ToInt32(-join ($_.ManufacturerName[0..2]).ForEach({[Convert]::ToString($_ - 64,2).PadLeft(5,'0')}),2)
            Manufacturer = (Convert-BigEndianToLittleEndian ($_.bigEndianInt32) -shr 16)

            ProductID = [int]::Parse(-join ($_.ProductCodeID[0..3]).ForEach{[char]$_}, [System.Globalization.NumberStyles]::HexNumber)
            Model = [System.Text.Encoding]::ASCII.GetString($_.UserFriendlyName).Replace("$([char]0x00)","")
        }
    }
)

$index = 1
foreach ($Monitor in $Monitor_List) {
    'EDID_{0}_{1}_{2}_{3}' -f $Monitor.Manufacturer, $Monitor.ProductID, $Monitor.Model, $DisplayPathDigit

i cant get it to print the ManufacturerName string. it outputs 0. the ProductID and Model do print correctly. do you know whats missing in the code for the ManufacturerName to print?

i also have to figure out how to output the EDID string like before, but with it knowing which monitor you selected. i tried this and it doesnt work either.

Code:
$MonitorChoice = $Monitor_List[$choice - 1].'EDID_{0}_{1}_{2}_{3}' -f $Monitor.Manufacturer, $Monitor.ProductID, $Monitor.Model, $DisplayPathDigit

how can i also create a variable that knows the $Monitor_List selection, and apply the EDID info for it?
 

My Computer My Computer

At a glance

Windows 11 27965Intel i7 7700 @4.0ghz64gb DDR4Radeon RX 5500 XT
OS
Windows 11 27965
Computer type
PC/Desktop
CPU
Intel i7 7700 @4.0ghz
Memory
64gb DDR4
Graphics Card(s)
Radeon RX 5500 XT
Sound Card
Topping D50s
Hard Drives
NVMe
PSU
Corsair
Keyboard
Stelseries G6v2
Mouse
Zowie EC2
Other Info
https://www.youtube.com/shoober420
Some displays don't return any correct WMI data. My laptop has a built-in LCD, and external HDMI. The LCD always causes my test scripts to error.

You can probably check that the Manufacturer value is null, or the converted string returns null and skip that device.
 

My Computer My Computer

At a glance

Windows 7
OS
Windows 7
i found a solution. i kept the original code and added a "Where-Object" to the string and now it selects the correct monitor when using the $selectedDisplayPath variable.

Code:
foreach ($Monitor in @(Get-WmiObject -Namespace root\wmi -Class WmiMonitorID | Where-Object {$_.InstanceName -like $selectedDisplayPath})) {

im very greatful for your support and giving me free powershell lessons lol, i definitely feel i have a deeper understanding of powershell and programming in general
 

My Computer My Computer

At a glance

Windows 11 27965Intel i7 7700 @4.0ghz64gb DDR4Radeon RX 5500 XT
OS
Windows 11 27965
Computer type
PC/Desktop
CPU
Intel i7 7700 @4.0ghz
Memory
64gb DDR4
Graphics Card(s)
Radeon RX 5500 XT
Sound Card
Topping D50s
Hard Drives
NVMe
PSU
Corsair
Keyboard
Stelseries G6v2
Mouse
Zowie EC2
Other Info
https://www.youtube.com/shoober420
Sometimes you stare at problem with no clear solution, walk away, and a better answer comes up in your head. In the end, it's still you doing the real work because any idea you borrowed from someone isn't going to write itself.

Unless you lifted an entire code block from Superuser :wink:.
 

My Computer My Computer

At a glance

Windows 7
OS
Windows 7

Latest Support Threads

Back
Top Bottom