Monday, August 29, 2016

How to run commands on multiple files with forfiles command

Forfiles is a useful windows command to select a set of files and then run a command on each of the files. It’s similar to the functionality of find command on Linux OS.
forfiles <Criteria to select files> /C "command to applied on each of files selected"
The criteria we can use to select files:
(Option /D) Modifies date or number of days it was last modified from
(Option /M) Search files based on names
(Option /S) Look for files in sub-directories also
(Option /P) Look for files in Specific directory

Find all excel files modified 10 days back in the current folder and subfolders

forfiles /D -10 /S /M *.xlsx /C "cmd /c echo @path"


In the above command @path is used to print the complete absolute path of the file. Similarly we can use below variables in the command part.
@file : Name of the file includes extension
@fname : Name of the file excludes extension
@relpath : Relative path of the file from the current folder
@ext : Extention of  the file
@fsize : Size of the file
@fdate : Last modified date of the file
@ftime : Last modified time of the file

delete all log files created in the last 1 month

forfiles /D +30 /S /M *.log /C "cmd /c del @file"

Copy/Backup files modified after 1st Jauary 2015

forfiles /D 01/01/2015 /M * /C "cmd /c copy @file D:\backupfolder\"

Get list of all picture files with their size

forfiles /S /M *.jpeg /C "cmd /c echo @path @fsize"

Get list of all exe’s and their last modified date

forfiles /M *.exe /C "cmd /c echo @path @fdate"

Get list of files which is less than given size

forfiles /S /M *.txt /C "cmd /c if @fsize LSS 1000 echo @file"

List all sub-directories in a given folder

forfiles /M * /C "cmd /c if @isdir==TRUE echo @file"

Recursively rename file extentions

If you want to rename files from one extension to another, recursively in all sub folders, then you can use the below command.
forfiles /S /M *.ext1 /C "cmd /c rename @file @fname.ext2"

Recursively remove file extentions

forfiles /S /M *.ext /C "cmd /c rename @file @fname"

Recursively add prefix in filenames

forfiles /S /M *.jpg /C "cmd /c rename @file photo@file"

How to user Systeminfo Command by filter its information

By Systeminfo command you can get following information.

Computer name, OS version, OS configuration, OS type, Install Date, System uptime data, BIOS version, Available physical memory, Processor model, Hotfixes installed, Network cards information, Domain name of the computer, System Locale, Time Zone and many other details.
if we want to get any particular information we can use findstr command to filter out unwanted details--------------------------
To get system’s physical memory information
systeminfo | findstr /C:”Total Physical Memory”
To get System type from windows
systeminfo | findstr /C:”System type”
To find System locale
systeminfo | findstr /C:”System Locale”
To find system manufacturer
systeminfo | findstr /C:”System Manufacturer”
To find OS install date
systeminfo | findstr /C:”Install Date”
To find System uptime

systeminfo | findstr /C:”Up Time”



Systeminfo Syntax :
systeminfo[.exe] [/s Computer [/u Domain\User [/p Password]]] [/fo {TABLE|LIST|CSV}] [/nh]

Parameters

/s   Computer   Specifies the name or IP address of a remote computer (do not use backslashes). The default is the local computer.
/u   Domain \ User   Runs the command with the account permissions of the user specified by User or Domain\User. The default is the permissions of the current logged on user on the computer issuing the command.
/p   Password   Specifies the password of the user account that is specified in the /u parameter.
/fo { TABLE | LIST | CSV Specifies the format to use for the output. Valid values are TABLELIST, and CSV. The default format for output is LIST.

/nh   Suppresses column headers in the output. Valid when the /fo parameter is set to TABLE or CSV.
The following examples show how you can use the systeminfo command:
systeminfo.exe /s srvmain
systeminfo.exe /s srvmain /u maindom\hiropln
systeminfo /s srvmain /u maindom\hiropln /p p@ssW23 /fo table

Thursday, August 18, 2016

How to disable file sharing in windows

In Windows, one can share files to other users in the same network using SMB protocol. We can disable sharing of a folder/drive from command prompt using net share command.

Open elevated windows command prompt and run the below command to see shares
c:\>net share
To disable sharing C$, run the below command
c:\>net share C$ /DELETE
To disable file sharing on the computer
c:\>net stop lanmanserver
Note: Stopping this service also stops Computer Browser Service

Happy Sharing...

How to set default printer for local or remote user

Set Default Printer Via Command Line

to get the printer name
wmic printer get name

for local user

cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -t -p <printer name>


RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "printer"

rundll32 printui.dll,PrintUIEntry /y /q /n “Printer name”

set a user's default printer for remote user
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n"\\JSI001.JSIINC.COM\HP Business Inkjet 2250 (PCL5C)"

wmic to set default printer

get the list of printers installed on the computer

wmic printer get name,default
on remote pc:
cscript prnmngr.vbs -l [-s RemoteComputer] [-u UserName -w Password]

get the default printer name
On local pc

wmic printer where default='TRUE' get name
On remote pc
cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -g -s <hostname>

set the default printer

wmic printer where name='printername' call setdefaultprinter
on remote:
cscript prnmngr.vbs -t -p PrinterName -s <hostname>

To delete the printer named ColorPrinter_2 from the remote computer named HRServer, type:
cscript prnmngr.vbs -d -s HRServer -p ColorPrinter_2
To delete all of the printers from a computer
cscript prnmngr.vbs -x [-s RemoteComputer] [-u UserName] [-w Password]
cscript prnmngr.vbs -d -p PrinterName [-u UserName -w Password] [-s RemoteComputer]
To add a printer named ColorPrinter_2 that is connected to LPT1 on the local computer and requires a printer driver called Color Printer Driver1, type:
cscript prnmngr.vbs -a -p ColorPrinter_2 -m "Color Printer Driver1" -r lpt1:
To add a printer connection

Syntax

cscript prnmngr.vbs -ac -p PrinterName 
-ac   Required. Specifies that you want to add a printer connection.
To add a local printer

Syntax

cscript prnmngr.vbs -a -p PrinterName [-s RemoteComputer-m DriverName -r PortName [-u UserName -w Password]
https://technet.microsoft.com/en-in/library/bb490973.aspx


Wednesday, August 17, 2016

Handling print jobs

To delete printjobs of specific printer

wmic printer where name="HP LaserJet" call cancelalljobs

To see the list of jobs we have in the queues
wmic printjobs get

To delete printjob

wmic printjob where jobid=<jobnumber> delete
Pause a print job

wmic printjob where jobid=<jobnumber> pause

prints lot of details about each printer job
wmic printjob get jobid, document, jobstatus

Resuming a print job


wmic printjob where jobid=<jobnumber> resume

Handling print jobs

To delete printjobs of specific printer

wmic printer where name="HP LaserJet" call cancelalljobs

To see the list of jobs we have in the queues
wmic printjobs get

To delete printjob

wmic printjob where jobid=<jobnumber> delete
Pause a print job

wmic printjob where jobid=<jobnumber> pause

prints lot of details about each printer job
wmic printjob get jobid, document, jobstatus

Resuming a print job


wmic printjob where jobid=<jobnumber> resume

uninstall a program with command

To know the name of already installed software in your computer

wmic product get name

and to uninstall software by their name

wmic product where name="windows live writer" call uninstall

To terminate a running program without asking for save

wmic process where name="iexplore.exe" call terminate
wmic process where name="notepad.exe" call setpriority 64
wmic /node:steve-pc service list brief