5

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.

enter image description here

aiden87
  • 929
  • 8
  • 25
  • 52

2 Answers2

5

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!

Cookie Monster
  • 1,741
  • 1
  • 19
  • 24
  • caption is indeed a better choice, thanks on that...but still it doesnt return any results. can you please check my updated post? – aiden87 Dec 05 '14 at 13:02
2

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.

Zombo
  • 1
  • 62
  • 391
  • 407