Skip to content

Instantly share code, notes, and snippets.

@santisq
Last active October 5, 2025 19:19
Show Gist options
  • Select an option

  • Save santisq/4afe32328ee5da7e4b3f80dd9b373c9d to your computer and use it in GitHub Desktop.

Select an option

Save santisq/4afe32328ee5da7e4b3f80dd9b373c9d to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Linq;
using System.Management.Automation;
using System.Reflection;
using System.Text;
using Microsoft.PowerShell.Commands;
using PowerShell ps = PowerShell.Create().AddCommand("Get-ComputerInfo");
ComputerInfo info = ps.Invoke<ComputerInfo>()[0];
PropertyInfo[] props = typeof(ComputerInfo).GetProperties();
int max = props.Max(e => e.Name.Length);
foreach (PropertyInfo prop in props)
{
Console.WriteLine(
$"\e[92m{prop.Name.PadRight(max)} :\e[0m {prop.GetValue(info)}");
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<NoWarn>MSB3277</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.5.2" />
<Reference Include="Commands.Management">
<HintPath>C:\Program Files\PowerShell\7\Microsoft.PowerShell.Commands.Management.dll</HintPath>
</Reference>
<Reference Include="ServiceProcess.ServiceController">
<HintPath>C:\Program Files\PowerShell\7\System.ServiceProcess.ServiceController.dll</HintPath>
</Reference>
<Reference Include="System.Management">
<HintPath>C:\Program Files\PowerShell\7\System.Management.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
@santisq
Copy link
Author

santisq commented Oct 3, 2025

image

@mklement0
Copy link

That's curious - I tried your exact code, except that I used v7.5.3 (both the SDK and the standalone PS 7 installation), and it doesn't work for me (Microsoft Windows 11 Pro (ARM 64-bit Processor; Version 24H2, OS Build: 26100.6584)).

@santisq
Copy link
Author

santisq commented Oct 3, 2025

That's curious - I tried your exact code, except that I used v7.5.3 (both the SDK and the standalone PS 7 installation), and it doesn't work for me (Microsoft Windows 11 Pro (ARM 64-bit Processor; Version 24H2, OS Build: 26100.6584)).

@mklement0 indeed, 7.5.3 doesn't work for me either

@mklement0
Copy link

Thanks for confirming; mysterious.

I've summarized the findings so far in a footnote to https://stackoverflow.com/a/79778070/45375 (which now links to this Gist).

If you come across further information, please leave a comment there.

@santisq
Copy link
Author

santisq commented Oct 3, 2025

Thanks for confirming; mysterious.

I've summarized the findings so far in a footnote to https://stackoverflow.com/a/79778070/45375 (which now links to this Gist).

If you come across further information, please leave a comment there.

@mklement0 If he doesn't know then we're surely out of luck 😅

image

@mklement0
Copy link

Thanks; I appreciate the follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment