how to get OS name in Windows Powershell using functions
Solution 1
You have omitted a critical part of your screen, with your image. What is important is the line directly after the last line shown. If the last line shown is truly the last, then you still need to press Enter once more. Consider this command
PS > 'hello world'
hello world
Notice the result printed as soon as I hit Enter. However certain actions, like defining a function cause PowerShell to enter interactive mode. Once PowerShell is in interactive mode it will require pressing Enter twice to start the evaluation. Example
PS > function foo {
>> echo bar
>> }
>> 'hello world'
>> 'dog bird mouse'
>>
hello world
dog bird mouse
Notice this time around I was able to enter a command after the same
'hello world'
command.
Solution 2
Try exploring the object to find out what property you want:
Get-WmiObject Win32_OperatingSystem | select -Property *
You will notice the 'Caption' property contains the friendly OS name, as micky-balladelli mentioned. Your example would change to:
Function Get-OSName
{
(Get-WmiObject Win32_OperatingSystem).Caption
}
Cheers!
aiden87
Updated on June 04, 2022Comments
-
aiden87 about 2 years
I'm trying to return OS name using functions in Windows Powershell.
I built this code, but i don't get any results. Any help please?
Function Get-OSName { (Get-WmiObject Win32_OperatingSystem).Name } "Name of the OS: $(Get-OSName)"
Thank you.