Hi
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
“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.
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.