Set csv file as global variable

Is there a possibility to use a global variable for csv file assignment?

basically I want to make sure that when my script runs multiple times it grabs 1 csv file which will be handled throughout the complete script.
So the first run of this script will have a global variable with the filename MickyMouse when the script is launched 2 min later through a new instance the csv file will have the name Donald Duck.

however when I do an import-csv like
$file = import-csv global:newstartersfile it throws me an error.
this is my code

$Path = 'C:\temp\csv\onboarding\test'
$ChildItems = Get-ChildItem $Path -Recurse -File
$finalworkfile = $ChildItems | Sort-Object LastWriteTime -descending | Select-Object -First 1 FullName, LastWriteTime
$newstarters = $finalworkfile.FullName
$onboardingfile = $newstarters
$global:newstartersfile = $onboardingfile
$test = get-content $global:newstartersfile

$file = import-csv $global:newstartersfile

what do I need to modify so that the csv file becomes a global variable which I can use to import where I want

“Global” just means that a variable exists in all scopes in the one PowerShell console session you declared this variable. If you close this session the variable is gone. You could make this variable permanent by adding its assigment to one of you profiles.

Another option would be to write it to the registry or a settings file like an INI file or a PSD1 file or a JSON file or something like this. This way you could check the settings file or the registry value at the beginning of your script and use it if it’s there.

Hi Olaf,
I understand question is if there is a possibility to use this global variable on a csv file which is going to be treated with foreach function when I tried this I got a delimiter error and don’t know how to solve that.
If I just ask to get-content of the global variable then I get the complete content of the csv file in question.

I doubt that a little bit, sorry. :wink:

I didn’t get that. If you use another delimiter than a comma you can specify it with the parameter -Delimiter.

Could you try to describe the task you’re trying achieve - not what you think you need to achieve this task? :wink:

As Olaf said, you will need to look at variable scoping. Your code is a tad confusing. You are defining the same item to multiple variables. Hard to figure out exactly what you are trying to do.