This is a quick example that shows how you can call a function in PowerShell. The example has a function that accepts an Active Directory (AD) Organisation Unit (OU) name, enumerates the computers within that OU and kills any process with the name “Application1.exe”.
Create two files in a folder called PowerShell in the root of C:\, one called KillProcess.ps1, which will contain the function and another called EnumPCsKillProcess.ps1, which will call the function. Below is the contents of each file:
ForEach ($child In $ou.psbase.Children)
if ($child.ObjectCategory -Like ‘*computer*’)
$Procs = gwmi -ComputerName $child.Name -Query “Select * from Win32_Process Where Name = ‘Application1.exe'”
ForEach ($Proc In $Procs)
$ou = [ADSI]”LDAP://OU=ENTER 1ST OU PATH HERE”
$ou = [ADSI]”LDAP://OU=ENTER 2nd OU PATH HERE”
The key line is the one in red above, which references the KillProcess.ps1 file, which contains the function.