Bookmark and Share

In this post:

 

How Can I Use the Ver Command in Windows PowerShell?

Hey, Scripting Guy! Question

Hey, Scripting Guy! You guys rock! I can’t believe you even got your wife using Windows PowerShell. Way to go! I wish my wife was a scripting wife. Anyway, I want to thank you for all your hard work on putting together the 2010 Scripting Games. I do have a couple of questions that have been bugging me:

1. Within a command window, I can find the version by typing "ver"; what is the equivalent command in Windows PowerShell?

2. Why is Windows PowerShell 2.0 installed in the directory "C:\WINDOWS\system32\windowspowershell\v1.0"?

-- MN

 

Hey, Scripting Guy! AnswerHello MN,

Microsoft Scripting Guy Ed Wilson here. I am glad you like the Scripting Wife; I also like her. Yes, the 2010 Scripting Games were a lot of work, but they were worth it. Here are answers to your questions.

In Windows PowerShell 2.0 there is an automatic variable called $PsVersionTable that returns a hash table:

PS C:\> $psversiontable

Name                           Value
----                           -----
CLRVersion                     2.0.50727.4927
BuildVersion                   6.1.7600.16385
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1


PS C:\>

The problem is that it returns the version of Windows PowerShell, and not the version of Windows that you see when opening a command prompt and typing ver as seen in the following image.

Image of ver returning version of Windows being used

One of the cool things about Windows PowerShell is that it is extremely configurable. This means that if a command does not exist, you can create your own command. MN, I decided to create a function that returns the Windows version in a manner that is similar to the one you see when running ver at a command prompt. The reason I say similar is because my function is better than the native ver command. First, my function also tells you what kind of Windows 7, as seen in the following image. In addition, my function allows you to retrieve the version of Windows on a remote computer. Best of all, it is still called ver 

Image of results of Ed's ver function

To make the most of the Get-Version function, you may wish to add it to your profile. If you do this, you will not want to copy the command that calls the function, but you will want the Set-Alias command. For more information about using profiles in Windows PowerShell, see the Profile Week Hey, Scripting Guy! posts from the week of November 23, 2009. The complete get-version.ps1 script is shown here.

get-version.ps1

function get-version ($computer = ".")
{
 $a = Get-WmiObject -Class win32_operatingsystem -ComputerName $computer
 $a.caption + "[" + $a.version + "]"
} # end function get-version

# *** start script ***

Set-Alias -Name ver -Value get-version -Description "mred alias"
get-version -computer hyperv

MN, in regard to your second question, the Scripting Wife recently asked the same question, so I will refer you to that article. 


What Do These Numbers Mean That Are Returned by This WMI Class?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I am trying to use WMI to retrieve the NDIS version on specific network interface cards. I retrieve the WMI class NdisCoDriverVersion (root\WMI\MSNdis_CoDriverVersion) and get values such as 1280, 1281, 1282. What are these numbers equivalent to? I have been searching the Web and cannot find any information.

-- ED

 

Hey, Scripting Guy! AnswerHello ED,

Interestingly enough, your initials are the same as my first name. The WMI classes in the root\wmi namespace are not documented, so I cannot find any information on MSDN. The reason they are not documented is they are automatically generated from hardware driver files that reside on the computer, and the various hardware makers write the drivers that in turn become these classes. Sometimes the things are understandable, and other times they are not.

You can find useful information about the class by looking at the class in Wbemtest. Here is an article that talks about using Wbemtest. Wbemtest, or the WMI Tester, is one of my favorite tools.

On my computer, the class in question returns a single number that looks like it is the driver version. If you are getting back three numbers, that is really strange. What would be interesting to do would be to see if two computers running the same operating system with the same patches return the same value. In fact, I just tested it on two computers that have the same NIC; one is running Windows 7 and the other Windows 2008 R2 running in core mode. They return the same value for the same adapter. Therefore, it might be useful informationI am just not completely certain.



How Can I Change the Color of Text Displayed in the Windows PowerShell Console?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have the following code in my script:

$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", ` "Continue Script and Allow Changes to AD"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", ` "Cancel Script"
$choices = [System.Management.Automation.Host.ChoiceDescription[]] ( $yes, $no )
$caption = "Warning! This will make changes to AD."
$message = "Are you sure you want to continue?"
$result = $Host.UI.PromptForChoice($caption, $message, $choices, 1)

I would like to change the $caption variable to be a different color, such as red or yellow, to bring attention to the message. Is this possible?

-- RL

 

Hey, Scripting Guy! AnswerHello RL,

Use the Write-Host cmdlet to display text in a different color on the Windows PowerShell console:

PS C:\> write-host -ForegroundColor red "Warning you are getting ready to make changes..."
Warning you are getting ready to make changes...
PS C:\>

As you can see here, there are no options to display the choice portion of PromptForChoice in a different color:

PS C:\> $host.ui | get-member -Name promptforchoice | format-list *


TypeName   : System.Management.Automation.Internal.Host.InternalHostUserInterface
Name       : PromptForChoice
MemberType : Method
Definition : int PromptForChoice(string caption, string message, System.Collections.ObjectModel.Collection[System.Manag
             ement.Automation.Host.ChoiceDescription] choices, int defaultChoice), System.Collections.ObjectModel.Colle
             ction[int] PromptForChoice(string caption, string message, System.Collections.ObjectModel.Collection[Syste
             m.Management.Automation.Host.ChoiceDescription] choices, System.Collections.Generic.IEnumerable[int] defau
             ltChoices)



PS C:\>

 

How Can I Get the User Name That Runs Services on Local and Remote Systems?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have been able to do just about anything I want with Windows PowerShell, but there is one thing that is getting away from me. I am unable get the user name that runs services on local and remote systems. Looking at the WMI interface, the data does not seem to be there either. Is there an easy way to get this information using Windows PowerShell?

-- EN

 

Hey, Scripting Guy! AnswerHello EN, 

The startname property from Win32_service is not displayed by default. The following command works on my Windows 7 laptop (you can also type this command: gwmi Win32_Service | ft state, startname –auto):

Get-WmiObject -class win32_service | Format-Table -property state, startname, name -auto

 

 

How Can I Create Dialog Boxes with Windows PowerShell?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I want to know if it is possible to create the following items in Windows PowerShell:

·         A static dialog box with specific buttons (for example, B1, B2, B3, B4) with response (True, False, Cancel, OK, No).

·         A dynamic dialog box: When one new host is added to the map, a dynamic button is created in the dialog box.

In VBScript, there are some free tools that can create these requirements automatically and the associated code. Are there some graphic tools in Windows PowerShell that can do this? I want to combine "graphics windows," Windows PowerShell code, and internet code such as HTA, HTML, and so on.

Thanks for your ideas/help.

-- RM

 

Hey, Scripting Guy! AnswerHello RM, 

I wrote a week’s worth of Hey, Scripting Guy! posts that used graphical components. There are at least three free graphical projects that may be of interest to you. The first is called PowerBoots; the second is part of the PowerPack Modules; and the third is available from SAPIEN Technologies and is called Primal Forms. There may be others, but this will get you started.

 

Well, this concludes another edition of Quick-Hits Friday. Join us tomorrow for the Weekend Scripter as we delve into the mysteries of…well, we will let that remain a mystery for now.

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys