how to run program with command line arguments for each object in object list in powershell?

Topics: User Forum
Mar 31, 2014 at 7:47 AM
hi i am not a guru in PS so I decided to turn to you for help.

I want to write on PS sript that will read a list of computers and on each run delprof2 with command line parameters.

I was trying to write a script like this:

$ServerList = Get-Content D:\server_list.txt
$delprof = 'c:\delprof2.exe'
$parm = ' /i/id:username'
ForEach ($server in $ServerList){&$delprof$parm}

but it did not work =(
Apr 12, 2014 at 2:22 AM
You are more likely to get a quicker answer asking this question on with the tag "PoweShell". That said, the answer to your question depends on whether each computer has PowerShell and if you can enabled psremoting on each computer. If you can do that AND you can run the script with admin credentials that also work on each computer (assumes your on AD) this should work:
$ServerList = Get-Content D:\server_list.txt 
$delprof = 'c:\delprof2.exe' 
$parm = ' /i/id:username' 
Invoke-Command -ComputerName $serverList {
    copy-item \\server\share\delprof2.exe c:\
    delprof2.exe /I /id:$username } -Arg $someUserName
If you can't meet the conditions of remoting-enabled PowerShell on each computer you can try using psexec:
foreach ($server in $ServerList) {
    psexec \\$server -u $adminusername -p $adminpassword -c -f c:\delprof2.exe /i /id:$username