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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:24:32+00:00 2026-05-30T13:24:32+00:00

I have a powershell script with the following code in it… $appdir = Split-Path

  • 0

I have a powershell script with the following code in it…

$appdir = Split-Path -Path $MyInvocation.MyCommand.Path
$xfrdir = $appdir + "\xfr\"
$cfgFile = "ofx_config.cfg"
$cfgPath = $appdir + "\" + $cfgFile
$configData = New-Object System.Collections.ArrayList
#  --- some other code here...
function Load-Config () 
{
    if (test-path ($cfgPath))
    {
        $configData = Import-Clixml -path "$cfgPath"
    } 
}
#  ---some other code here
load-config

When I just RUN this script in the ps ISE, load-config runs because it is at the end of script (i verified this with a breakpoint) but the $configData variable remains empty.

But if I immediately copy and past the line $configData = Import-Clixml -path "$cfgPath" into the powershell command line and run it then $configData is loaded with data. Does anyone have any ideas what might be going on?

EDIT
I think that what you are saying is that $configData in $configData = Import-Clixml -path "$cfgPath" is being treated as a whole separate variable (and is local to the function) because of scoping rules. I thought it would be more like a c# class and so would assign to the script level variable of the same name.

I am LOVING powershell but dynamic typing does make things trickier.

EDIT 2
Both answers were insightful. In such a case I usual give the answer to the person with the fewest reputation. And I did in fact Andy’s second example in any case.

Seth

  • 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-05-30T13:24:33+00:00Added an answer on May 30, 2026 at 1:24 pm

    You have a scoping issue with variable $configData. When you set it’s value in the function it is not available outside. You can use a scope modifier to fix it or return the value.

    Check out get-help about_scopes or click here.

    Scope Modifier:

    $cfgPath = 'C:\Test.xml'
    $script:configData = New-Object System.Collections.ArrayList
    
    function Load-Config () 
    {
        if (test-path ($cfgPath))
        {
            $script:configData = Import-Clixml -path "$cfgPath"
        }
    }
    load-config
    $configData
    

    Note – Your ArrayList gets overwritten using Import-Clixml and is a different type on return.

    Return the new value:

    $cfgPath = 'C:\Test.xml'
    function Load-Config () 
    {
        if (test-path ($cfgPath))
        {
            $data = Import-Clixml -path "$cfgPath"
            return $data
        }
    }
    $configData = load-config
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a following script in PS: [System.Xml.XmlDocument] $Config; function Get-ScriptDirectory { Split-Path $script:MyInvocation.MyCommand.Path
I have the following snippet of PowerShell script in my profile: . (join-path (split-path
I have the following code snippet from my PowerShell script that... Loops through a
I have the following string expression in a PowerShell script: select count(*) cnt from
I have the following PowerShell script that will parse some very large file for
I have a powershell script in which I do the following $somePSObjectHashtables = New-Object
I have the following tiny PowerShell script that's meant to kill some certain processes
I have the following powershell script which loads a custom .NET DLL this contains
I have the following script set as my VS2010 pre-build script: cd $(ProjectDir) C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe
I have the following powershell script: param( [Int32[]] $SomeInts = $null, [String]$User = SomeUser,

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.