Need a PowerShell Script to determine if the current box is running Server 2008 or 2012 and run a specific function based on result


I have two scripts to perform the same function. One runs smoothly on Server 2008 R2, the other smoothly on 2012 R2. I want to have the script first check to see which OS is running and perform either the 2008 tasks, or the 2012 tasks.

I've spun my wheels on trying to get this to work. Any help? :D

This was as far as I got and this didn't work.

$check = (Get-WmiObject -class Win32_OperatingSystem).Caption
If ($OScheck.Caption -contains "8") {

If ($OScheck.Caption -contains "2008") {
Test 2

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try it like this:
$OSCaption = (Get-WmiObject -Class Win32_OperatingSystem).Caption
If ($OSCaption.Contains("2008")) {
} ElseIf ($OSCaption.Contains("2012")) {

Open in new window

Note that "-contains" is an operator that checks whether an array contains an element, not a substring based "contains". When used on strings, it's basically the same as -eq.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
iVenture_SolutionsAuthor Commented:
Thanks for your help! That did the trick. Very good to know about "contains!"
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.