Forum Discussion
pra4ash
Jun 03, 2020Copper Contributor
Variable not appearing in Function
Guys - newbie here, please bear with me.
1. I have a function that'll store user credential. It's in the profile file
2. profile loads, I initiate the function manually Get-MyUserCred
3. Loads successfully and exits the function
4. Why am I not able to use the variable ($cred) from that function? It just doesn't work, however, if I remove the function from the profile file, it works.
Not sure if this makes sense to you guys, but any help is much appreciated!
Thank you
#Prakash
- Manfred101Iron Contributor
pra4ash This is because of Powershell scopes. You can read about scopes here: https://ss64.com/ps/syntax-scopes.html
In your case you have to use $global:Cred = Get-Credential to make the $Cred available outside your function!
Grtz, Manfred de Laat