Link Menu Search Expand Document

Powershell

Table of contents

  1. Download File
  2. Unzip File
  3. Run program as other user
  4. Killing a process

Download File

powershell.exe -nologo -noprofile -command "(new-object System.Net.WebClient).Downloadfile(\"http://10.11.0.46/PSTools.zip\", \"c:\temp\PSTools.zip\")"

Unzip File

powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('PSTools.zip', '.'); }"

Run program as other user

runas

Download PSTools from sysinternals and use:

psexec \\machine_name -u user -p password program.exe programargs

Killing a process

It is so DAMN HARD to do this in Powershell.

Here are some different ways of doing it:

(Get-WmiObject win32_process | ? {$_.commandline -like "*firefox*"}).Terminate()

OR

$pattern = '-L 5432:localhost:5432 -L 9200:localhost:9200 -L 54321:localhost:54321 -f -N serverip'
$process = Get-WmiObject -Class win32_process  | Select-Object -Property ProcessId, CommandLine | Select-String "$pattern"
$pid_ = [regex]::Match($process.Line, 'ProcessId=(\d+)').Groups[1].ToString()
Stop-Process -Force -Id $pid_

Gilgalab Knowledge Base