Dot-sourcing is a concept in PowerShell that allows you to reference code defined in one script.
When you writing large size of PowerShell scripts, then there always seems to come a time when a single script just isn’t enough.ย If we working on large size of script then it important to keep your scripts Simple and Reusable, so you can keep your block of script as modular. The Dot-sourcing is a way to do just that. Making script as modular it also useful when requirment came for adding some more functionlaiyt in exting script. Other than building a module, dot-sourcing is a better way to make that code in another script available to you.
For example, let’s say I’ve got a script that has two functions in it. We’ll call this script CommunicateUser.ps1. Here i had created single module which contain the two functionality in single file which used for sending email and SMS to the customer.
CommunicateUser.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function SendEmail { param($EmailContent) #Write your Logic here Write-Output "****************************************" Write-Output "Sending Mail" $EmailContent Write-Output "****************************************" }` function SentSMS { param($SMSContent) #Write your Logic here Write-Output "****************************************" Write-Output "Sending SMS" $SMSContent Write-Output "****************************************" } |
UpdateUser.ps1
1 2 3 4 5 6 7 |
# My CommunicateUser.ps1 script were located on the root of my C drive, so I just put a period, followed by a space and then the path of the script. . C:\Users\MYPC\Desktop\PowerShell\CommunicateUser.ps1 $mailContent = 'MAIL - Hi Welcome to dotnet-helpers.com' $smsContent = 'SMS - Hi Welcome to dotnet-helpers.com' # Method Name (SendEmail) Parameter Name (-EmailContent) Parameter Value ($mailContent) SendEmail -EmailContent $mailContent SentSMS -SMSContent $smsContent |
OUTPUT:
What do you think?
I hope you have an idea of how to utilize the dot-sourcing concept in Powershell. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.
Leave A Comment