Skip to main content


Showing posts with the label Batch File

PowerShell remove line feed

Line feed is useful in some ways like formatting lines so it will be easy to read if there’s a line in between the other line.
But there are also instances that line feed might be annoying or simply we need to get rid of it since it’s not desired on the output.
There instances that Out-string parameter in PowerShell will append a line feed on its output.
Or if you take the input or output from other sources but want to remove the line feed, PowerShell can remove the line feed without any hassle.
Below is a simple way to remove line feed from an input variable.
$string_with_line_feed -replace "`n|`r"
`n -  backtick with n refers to the line feed character `r – backtick with r refers to the carriage return
Or the code above can be written as follows:
$string_with_line_feed -replace "`n|"
So, basically line feed is replace with nothing. But for readability and to avoid confusion it’s better to put what character is used to replace the line feed.

Check out link below, i…

Find or search files in Windows

Search files recursively using PowerShell or the old good dir command line.
To search using PowerShell:
gci d:\ -recurse | where-object {$ -like "g*.txt" }
GCI – a short form for Get-ChildItem Recurse – is to search recursively (folder and sub-folders) G*.txt - * tells the PowerShell to search any document that start with g and don’t care anything that follows it
To do it using the old way via command line, open command prompt and type:
Dir /s d:\ g*.txt
To save the search output just use the “>” redirect operator.
Dir /s d:\ g*.txt > d:\G_filenames.txt
/s - parameter to search recursively (folder and sub-folders)

The search result will be written to G_filenames.txt
In PowerShell to save the search result type:
gci d:\ -recurse | where-object {$ -like "g*.txt" } | out-file d:\gxfiles.txt

If you still want the very conventional way of searching.

Minimize all windows folders by pressing "windows key" + "d", win d (press both keys a…

Map drive not working

Mapping drive can be set in different ways,  via group policy, using scripting (PowerShell, Vbscript, Jscript) or it can be set via command line.
Setting script via command line can be done easily using ”net” command, like the example below. The example below shows the syntax on how to map a drive or folder.

net use ?
The syntax of this command is:
NET USE [devicename | *] [\\computername\sharename[\volume] [password | *]]         [/USER:[domainname\]username]         [/USER:[dotted domain name\]username]         [/USER:[username@dotted domain name]         [/SMARTCARD]         [/SAVECRED]         [[/DELETE] | [/PERSISTENT:{YES | NO}]]
NET USE {devicename | *} [password | *] /HOME
Net use \\AD_Server\Working_folder  Z:
The command will map “Working_folder” to drive Z on the client PC.
The above command will not need a password  if run as a batch file and deploy as a logon script via group policy.
To delete the mapped drive : net use Z: /delete

Disconnect Remote Desktop from command line

How to kill remote desktop sessions from the command line?
How to close RDC sessions from the command line?
One solution is to use batch file scripting or use PowerShell.
For old timers batch file might be the preferred solution because you don't need to install anything, it  comes in handy with the native command prompt.
Of course, PowerShell is also one of the best solutions; provided the environment is PowerShell ready.
The method used below utilizes batch file or the command prompt, just like the good old DOS environment of yesterday.
Command below can be run directly from the command prompt.
To use batch file scripting open notepad and save the file with ".bat"  file extension. Once saved as a batch file, the script is ready for automation with the help of Task Scheduler.
Here's the command:
for /f "tokens=2,5" %a in ('netstat -ano ^| find "3389"') do echo %a & tskill %b /v
For folks who just started to embark in batch file world,…