Comments on: PowerShell Reference Variable https://www.spguides.com/powershell-reference-variable/ Learn SharePoint, Office 365, Nintex, PowerApps, PowerBI etc, SharePoint training and video courses Fri, 05 Jan 2024 19:02:29 +0000 hourly 1 https://wordpress.org/?v=6.6.2 By: ALIENQuake https://www.spguides.com/powershell-reference-variable/#comments/1041 Sun, 24 Sep 2023 12:28:17 +0000 https://www.sharepointsky.com/?p=10242#comment-1041 This article is influenced by how to do things via c# but lacks of fundamentals regarding PowerShell. Firstly, that’s not the correct way to set a script-level variable. You should simply use PowerShell-way: $script:number. Secondly, this approach is not recommended for writing any kind of PowerShell code, as it is prone to mistakes. Lastly, the function names are a mess as they don’t use verb-noun format and they don’t actually do what they are called. A much better approach would be:
1) You aren’t doing any calculations via Calculate function
function Set-ValueTo50
{
50
}
$num = Set-ValueTo50
$num

2.1) Not optimal
function Set-BhawanaRathore
{
$script:FirstName = “Bhawana”
$script:LastName = “Rathore”
}

$FirstName = “Bijay”
$LastName = “Sahoo”
Write-Host “$FirstName $LastName”
Set-BhawanaRathore $FirstName $LastName
Write-Host $firstname” “$lastname

2.2) Optimized
function Set-FirstNameBhawana
{
“Bhawana”
}
function Set-LastNameRathore
{
“Rathore”
}

$FirstName = “Bijay”
$LastName = “Sahoo”
Write-Host “$FirstName $LastName”
$FirstName = Set-FirstNameBhawana
$LastName = Set-LastNameRathore
Write-Host $firstname” “$lastname

]]>