How to get OS name in Windows Powershell using functions

I am trying to return OS name using functions in Windows Powershell.

I have built this code, but I am not getting any results. Any help please?

Function Get-OSName
{
(Get-WmiObject Win32_OperatingSystem).Name
}
"Name of the OS: $(Get-OSName)"

      

Thank.

enter image description here

+3


source to share


2 answers


You missed a critical part of your screen with your image. What's Important is the line immediately after the last line shown. If the last line shown is indeed the last, then you still need to click Enteragain. Consider this command

PS > 'hello world'
hello world

      

Notice the result printed as soon as I click Enter. However, certain actions, such as defining a function, cause PowerShell to go online. When PowerShell is online, you need to Enter double- click to start the assessment. Example



PS > function foo {
>> echo bar
>> }
>> 'hello world'
>> 'dog bird mouse'
>>
hello world
dog bird mouse

      

Note that this time I was able to enter the command after the same 'hello world'

.

+3


source


Try examining the object to see what property you want:

Get-WmiObject Win32_OperatingSystem | select -Property *

      

You will notice that the "Caption" property contains the friendly name of the OS as mentioned by micky-balladelli. Your example will change to:



Function Get-OSName
{
    (Get-WmiObject Win32_OperatingSystem).Caption
}

      

Hooray!

+2


source







All Articles