I have a small problem (i think it’s a small problem.):
I created a small function with a code i found in the web to create a datepicker .I want reuse this code many times, for that i create a function datepicker, i would retrieve a string value inside this function but the value disappear outside the function, i don’t understand why :
One of the basic rules of scoping at the top of the link provided is this.
An item you include in a scope is visible in the scope in which it was created and in any child scope, unless you explicitly make it private. You can place variables, aliases, functions, or PowerShell drives in one or more scopes.
This means that because your $result is defined within your function (child of your script), it is only accessible within that function, and not in it's parent. By default scope access trickles down, but not up.
Try adding a line to the bottom of your function that returns your $result such as.
[pre]Write-Output $result[/pre] or [pre]return $result[/pre]
Then on line 42 your $dapicker variable will now hold the result value and you’ll no longer reference your $result variable outside of the function scope.
Write-host is only meant for the screen. Also variables in a function can't be seen outside it.
… as of PS5x, that is no longer true.
Yet agreed, Write-Host, outside of specific needs should not be used. You don’t even need Write-* to send to the screen. Write-Output is the default. So, all the below write to the screen without explicitly calling Write anything.
… being the go to statement. However, Jeffrey Snover change his stance on this as of May 2016. With PowerShell v5 Write-Host no longer “kills puppies”. Data is captured into info stream …