Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7817937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:24:16+00:00 2026-06-02T06:24:16+00:00

I’m currently creating an AD script that can get the AD groups of one

  • 0

I’m currently creating an AD script that can get the AD groups of one machine and transfer them to a new machine (in case of system failure).

I’ve managed to get the script to go out and find the versions of Windows that the two machines are running via their hostname, however I’m having a problem creating an ‘if’ statement to compare the two versions of Windows.

The idea is that if the are the same version (and thus the same package version) the groups will be copied automatically, but I can’t for the life of me figure out how to do it.

Please consider this code:

function W_version_current
{
    $current = Get-WmiObject Win32_OperatingSystem -computer $current_hostname.text | select buildnumber
    if ($current -match '7601')
    {
        "Windows 7"
    }
    elseif($current -match '2600')
    {
        "Windows XP"
    }
    elseif($current -eq $null)
    {
        "The box is empty"
    }
    else
    {
        "Function not supported"
    }
}

function W_version_target
{
    $target = Get-WmiObject Win32_OperatingSystem -computer $target_hostname.text | select buildnumber
    if ($var -match '7601')
    {
        "Windows 7"
    }
    elseif($target -match '2600')
    {
        "Windows XP"
    }
    elseif($target -eq $null)
    {
        "The box is empty"
    }
    else
    {
        "Function not supported"
    }
}

function compare_current_target
{
    if(W_version_current -eq W_version_target)
    {
        "Matching version of Windows detected"
    }
    else
    {
        "Versions of Windows do not match"
    }
}

Now is it true that all variables cannot be accessed outside of functions?

If so, what else can I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:24:17+00:00Added an answer on June 2, 2026 at 6:24 am

    Probably what you’re missing is that with PowerShell order of operations, you often have to put function calls in parentheses.

    Try this instead:

    if ((W_version_current) -eq (W_version_target))
    {
        "Matching version of Windows detected"
    }
    else
    {
        "Versions of Windows do not match"
    }
    

    To answer your question, scope in PowerShell works pretty much like most other scripting languages, e.g. variables declared in functions cannot be used outside of the functions they were declared, unless you declare them as global, which you can do like so:

    $global:x = "hi"
    

    You can then use the variable $x anywhere, or if you like, $global:x, and it will have the value of "hi".

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.