Executing Commands from PowerShell script

Executing Commands from PowerShell Script PS interface to run system internal or external commands is supported via invoke-* cmdlets as listed below: PS C:> get-command Invoke-* | ft name Name Invoke-Command Invoke-Expression Invoke-History Invoke-Item Invoke-WmiMethod Invoke-WSManAction PS C:> Executing Command on Local Machine: $executable = ‘c:WindowsSystem32sc.exe’ $parameters = ‘qc’ $arguments  = ‘WinRM’ $command    = “$executable $parameters $arguments” Invoke-Expression $command Execution Output: PS C:> $executable = ‘c:WindowsSystem32sc.exe’ PS C:> $parameters = ‘qc’ PS C:> $arguments  = ‘WinRM’ PS C:> $command    = “$executable $parameters $arguments” PS C:> Invoke-Expression $command [SC] QueryServiceConfig SUCCESS SERVICE_NAME: WinRM TYPE               : 20  WIN32_SHARE_PROCESS START_TYPE         : 3   DEMAND_START ERROR_CONTROL      : […]

Read more

PS: File/Folder Copying in PowerShell

Many times we require to copy a few bytes of single file to a few GBs of DB files on the other.  Here are basic PS cmdlets that help you script/automate the tasks safely in PS. Important Notes on Copy Behavior: Copy process merges the source files with destination path You need to use forceful copy if the destination folder already exists If destination folder doesn’t exist, creates destination folder with files under sources folder instead of copying the whole folder as is If destination folder exists, copy process copies the whole source folder as subfolder in the destination folder […]

Read more

PS: Check File/Folder/Drive/Path/Location Existence

In scripting very often we come across need to dynamically create a path (file/folder), for which you would like to perform a existence check before you attempt to create a folder. DRIVE existence/availability check: $drive = ‘Z:’ if (Test-Path -path $drive) {     write-host "$drive drive exists" } else {     write-host "$drive drive does NOT exists" } PS C:> if (Test-Path -path Z:) { write-host "Z: drive exists" } else { write-host "Z: drive does NOT exists" } Z: drive does NOT exists PS C:> FILE existence/availability check: $file= ‘C:TEMPTestFile.txt’ if (Test-Path -path $file) {     write-host "$file exists" […]

Read more