Variable not appearing in Function

%3CLINGO-SUB%20id%3D%22lingo-sub-1435761%22%20slang%3D%22en-US%22%3EVariable%20not%20appearing%20in%20Function%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1435761%22%20slang%3D%22en-US%22%3E%3CP%3EGuys%20-%20newbie%20here%2C%20please%20bear%20with%20me.%3C%2FP%3E%3CP%3E1.%20I%20have%20a%20function%20that'll%20store%20user%20credential.%20It's%20in%20the%20profile%20file%3C%2FP%3E%3CP%3E2.%20profile%20loads%2C%20I%20initiate%20the%20function%20manually%20Get-MyUserCred%3C%2FP%3E%3CP%3E3.%20Loads%20successfully%20and%20exits%20the%20function%3C%2FP%3E%3CP%3E4.%20Why%20am%20I%20not%20able%20to%20use%20the%20variable%20(%24cred)%20from%20that%20function%3F%20It%20just%20doesn't%20work%2C%20however%2C%20if%20I%20remove%20the%20function%20from%20the%20profile%20file%2C%20it%20works.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENot%20sure%20if%20this%20makes%20sense%20to%20you%20guys%2C%20but%20any%20help%20is%20much%20appreciated!%3C%2FP%3E%3CP%3EThank%20you%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23Prakash%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1435761%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1435860%22%20slang%3D%22en-US%22%3ERe%3A%20Variable%20not%20appearing%20in%20Function%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1435860%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F687598%22%20target%3D%22_blank%22%3E%40pra4ash%3C%2FA%3E%26nbsp%3BThis%20is%20because%20of%20Powershell%20scopes.%20You%20can%20read%20about%20scopes%20here%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fss64.com%2Fps%2Fsyntax-scopes.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fss64.com%2Fps%2Fsyntax-scopes.html%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20your%20case%20you%20have%20to%20use%26nbsp%3B%3CSTRONG%3E%24global%3ACred%20%3D%20Get-Credential%3C%2FSTRONG%3E%20to%20make%20the%20%24Cred%20available%20outside%20your%20function!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGrtz%2C%20Manfred%20de%20Laat%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

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

1 Reply

@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