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 8754919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:44:18+00:00 2026-06-13T13:44:18+00:00

The script is self-explanitory but I have no idea how to get it to

  • 0

The script is self-explanitory but I have no idea how to get it to work. I want to figure out how to pass the variable $ComputerPath to the function and in that script set $ComputerPath

Function CheckPath {
    While (!$args[0]) {
        Write-Host "`nVariable Undefined"
        $Args[0] = Read-Host "Enter Valid Path"
    } 
    while (!(test-path $Args[0])) {
        Write-Host "Unable To Location Files, Please Check Again."
        $args[0] = Read-Host "Enter Valid Path"
    }
}

$ComputersPath = "missingfile.txt"

$ComputersPath
CheckPath $ComputersPath
$ComputersPath

My Result

PS Z:\Powershell Scripting\Test Lab> .\TestFunction.ps1
missingfile.txt
Unable To Location Files, Please Check Again.
Enter Valid Path: log.txt
missingfile.txt
PS Z:\Powershell Scripting\Test Lab>
  • 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-13T13:44:19+00:00Added an answer on June 13, 2026 at 1:44 pm

    try pass the variabable to function like this:

    CheckPath $ComputersPath
    

    $ in single quote is see by powershell as literar ‘dollar sign’ and not a variable qualifier

    And change these lines:

    $args[0] = Read-Host "Enter Valid Path"
    CheckPath
    

    in

    CheckPath (Read-Host "Enter Valid Path")
    

    EDIT:

    Try this:

    Function CheckPath {    
        IF (!$args[0]) {    
            Write-Host
            Write-Host "Variable Undefined"
            $args[0] = Read-Host "Enter Valid Path"
        }
        else
        {
          "Found"
          $global:ComputersPath  = $args[0]
        }
    
    IF (!(Test-Path ($Args[0]))) {
        Write-Host
        Write-Host "Unable To Location Files, Please Check Again."
        CheckPath (Read-Host "Enter Valid Path")
    }
    

    }

    Edit:

    To set whatever variable you use in the function I give you a example:

    function test 
    {   
        $myvar = $MyInvocation.line.split(' ')[1].replace('$','')    
        "`$$myvar value now is $($args[0])"
    
        Invoke-Expression  "`$global:$myvar = 'yahoo'"    
    
        "{0} value now is {1}" -f "`$$myvar", (invoke-expression "`$$myvar")
    }
    

    after you can try this:

    PS C:\ps> $a="google" #or whatever variable you want...
    PS C:\ps> test $a
    $a value now is google
    $a value now is yahoo
    PS C:\ps> $a
    yahoo
    

    Now you can use the code in this function and put in your CheckPath function based on the logic of your goal

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

Sidebar

Related Questions

I get this error, but i can not figure out the fix for it:
Duplicate Why don’t self-closing script tags work? I'm writing an ASP.net page, which have
Possible Duplicate: Why don’t self-closing script tags work? I wonder why scripts included into
I have a form that submits to PHP self. This script runs and says
All of a sudden, this doesn't work for me: <script type=text/javascript> window.opener.startLoad(); self.close(); </script>
Possible Duplicate: Why don't self-closing script tags work? I've noticed that you can't slash-terminate
Possible Duplicate: Why don't self-closing script tags work? I'm trying jquery now. When I
Possible Duplicate: Why don't self-closing script tags work? I just found a weired behavior
Possible Duplicate: Why don't self-closing script tags work? I am using Spring web-mvc with
Possible Duplicate: Why don’t self-closing script tags work? I had this bit of javascript

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.