Referencing reusable code modules across scripts

A common use case is to code common functions into a stand-alone module then refer to that module from other code blocks to reduce code duplication. This is easily accomplished in PowerScripts.

For example, if you script produces a file as part of it’s output, you would like to create a download link so the user can easily get the file. Rather than duplicate this code in every script which outputs files, we have included with the product a script called “FormatforDownload Link”, shown below:

Anytime you create a script which produces a file, you can reference this function easily by selecting it as a Script Dependency.

Finally, in the script from which you want to call the shared function, you simply refer to the script name and pass it any required parameters, as shown below on line 8:

Write-Output "Starting to gather member list for $GroupName" Get-MsolGroupMember -GroupObjectId $GroupId -MaxResults 1000000 | Where-Object { $_.GroupMemberType -eq "User" } | Foreach { Get-MsolUser -UserPrincipalName $_.EmailAddress | Select DisplayName, UserPrincipalName, UserType, UsageLocation, Country, State, City, Department, IsLicensed } | Export-Csv -Path $filePath -NoTypeInformation -Encoding UTF8 Write-Output "All members found and exported to the CSV." Write-Output $(FormatForDownloadLink $filePath)

For a more advanced use case, see https://rprvitalsigns.atlassian.net/wiki/spaces/V4UG/pages/2353299464