My Most Commonly used Powershell Commands

Lately I have moved away from using vanila command line of windows to using powershell

For may tasks I find it useful. Some are still better in the old cmd.

Here are few commands that I have regularly used in powershell.

Create a zip file with the contents of C:\Stuff\

Compress-Archive -Path C:\Stuff -DestinationPath archive.zip

Add more files to the zip file (Existing files in the zip file with the same name are replaced)

Compress-Archive -Path C:\OtherStuff*.txt -Update -DestinationPath archive.zip

Extract the zip file to C:\Destination\

Expand-Archive -Path archive.zip -DestinationPath C:\Destination

Copy files folders

Copy-Item -Path C:\PointA\1.txt -Destination C:\PointB\

A real example, something that is part of a backup script

powershell.exe -NoP -NonI -Command "Copy-Item -Path '%root%\apps_installer' -Recurse -Destination 'path\to\directory' -Container -force"

To explore more, type powershell in your windows command prompt and then type help.

One of the best thing about powershell is it extensive help.

Advertisement

Some Common Schedule Tasks Command

I have been using schedule tasks in windows quite often and this has become a tool of choice for running many automation scripts on my work and home computer.

Here are few of the common scheduling tasks create commands that I most commonly use.

Below is how to schedule something to run at 7:00 every day every 1 hour for a duration of 1 day.

schtasks /create /tn "monitor" /tr "C:\Users\singh\Desktop\daytracker\activate_tracker.bat" /sc DAILY /st 09:45 /f /RI 60

Open tasks at certain time

SCHTASKS /Create /SC DAILY /TN checkout /TR "C:\Users\singh\Desktop\open_tasks.bat" /ST 16:15 /ET 17:15 /K

Run a particular task

schtasks /run /tn train

Use schtasks /create /? to see many other examples

Settings for Using Powershell without Non-Administrator Access

Recently created a powershell script and tried calling it from windows without admin access and this is the error message

(C:\Anaconda) C:\Anaconda>powershell -File C:\Users\Desktop\calc.ps1
File C:\Users\Desktop\calc.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see
about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
+ CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnauthorizedAccess

stumped…thats bad after few google searches and few stack overflow posts, this is the solution that worked for me.

set PWS=powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile

%PWS% -File C:\Users\u8040036\Desktop\calc.ps1

Can be seen in action in this shortcut script.

Creating Shortcuts in Windows in Batch Mode

Problem

A few months back, as part of an application deployment, needed to create shortcut in windows in batch mode

Solution

With google search and trials, this is the batch script that does that for me.

! create_shortcut.bat

@echo off
set WD=%1
set TARGET="%2"
set SHORTCUT="$home\Desktop\%3.lnk"
set PWS=powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile
%PWS% -Command "$ws = New-Object -ComObject WScript.Shell; $s = $ws.CreateShortcut(\"%SHORTCUT%\"); $S.TargetPath = \"%TARGET%\"; $S.WorkingDirectory = \"%WD%\"; $S.Save()"

How to call this:

create_shortcut.bat d:\apps http://www.google.com google

This will create shortcut to google on users desktop

  • First argument is start-in folder name
  • Second argument is the app or website the shortcut is pointing to.
  • Third argument is the name of the shortcut,

Scheduling Tasks in Windows with Command Line

Problem:

I run a neural network model that predicts task to perform on my system, and this neural network needs regular re training, so needed a system to run the training tasks on windows as a weekly job.

Solution:

In macos or linux, users have the convenience of cron to runs tasks in the background at specific times. There is nothing that simple in windows.

schtasks has a great GUI but this is mostly restricted by admins, so to use it, one needs to retort to the batch use.

here’s an exaple of its usage

Example: schtasks for each friday at 14:14

SCHTASKS /Create /SC WEEKLY /D FRI /TN train_grid_search /ST 14:14 /TR "C:\Users\singh\Desktop\predict_now\train_validate_model.bat"

Type schtasks /? in command line of windows to learn more.

Command Line Parameters for Your Batch Script

When using python as a driver program to lot of other apps, usage of batch files in windows system is something everyone encounters

And when it comes to batch files, sending command line arguments to batch file is one thing i tend to google a lot. A command line argument (or parameter) is any value passed into a batch script. So for convenience, I am listing the most common usages that I needed last year

%* for all command line parameters (excluding the script name itself).

%0 - the command used to call the batch file (could be foo, ..\foo, c:\bats\foo.bat, etc.)
%1 is the first command line parameter,
%2 is the second command line parameter,
and so on till %9 (and SHIFT can be used for those after the 9th).

%~nx0 - the actual name of the batch file, regardless of calling method (some-batch.bat)
%~dp0 - drive and path to the script (d:\scripts)
%~dpnx0 - is the fully qualified path name of the script (d:\scripts\some-batch.bat)

More info examples at https://www.ss64.com/nt/syntax-args.html