Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Variables persist outside of loops?

$
0
0

Hi,

I had a quick question about using variables inside of loops.

I noticed that if create a variable inside a foreach loop that the variable still exists once i am outside of the loop. This seems a bit odd to me because in most of the other languages that i have used, variables created inside a loop only exist inside that loop, this way you can reuse variable names without fear of carrying data over from a variable used in an other loop.

for example. I'm currently writing a script to handle AD user creation, modification, disabling and deletion. for each of these i have a foreach loop that will loop through the list of users that apply to it. Inside the loop i might want to have a variable called $currentUser, but i wouldn't want for it to be possible for a variable assigned to $currentUser in the user creation loop to show up in the user modification or deletion loop.

Hope this makes sense

thanks


Viewing all articles
Browse latest Browse all 13067

Trending Articles