Using PowerShell To Create Batch Command Calls

by Mar 18, 2009

Whenever you do something twice, automate it! This applies to native commands as well. PowerShell can help you call a native command repetitively. For example, here is how to construct a PowerShell loop if you'd like to use NETSH.EXE to create a bunch of DHCP reservations::

$description = "Internal_VLAN"
$segment = 44

$pcname = "$description-Reserved"
$ip1 = "15.77.$segment."
$scopename = "$($ip1)0"


10..126 | ForEach-Object {
$ip = "$ip1$_"
$mac = "001025{0:000}{1:000}" -f $segment, $_
#& "netsh dhcp server 10.25.64.22 scope $ScopeName add reservedip $IP $MAC $PCName"
"netsh dhcp server 10.25.64.22 scope $ScopeName add reservedip $IP $MAC $PCName"
}

It actually just composes the netsh command lines and outputs them as string. If you'd like to execute the commands, uncomment the comment line. PowerShell executes the string when you append a "&" to a string.