Command Line

How to Create All Users Desktop Shortcut from Command Line - Windows 7, 8 and 10

There isn't an easy way to create a shortcut from the command line on Windows. The easiest way we've found is to use a bat script to create vbs code, and then execute the code with cscript. It's actually really simple. Just paste the code below into a bat script and execute it to create a shortcut on the All Users desktop folder:

echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs
echo sLinkFile = "C:\Users\Public\Desktop\ShortcutToSource.lnk" >> CreateShortcut.vbs
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
echo oLink.TargetPath = "C:\sourceFolder" >> CreateShortcut.vbs
echo oLink.Save >> CreateShortcut.vbs
cscript CreateShortcut.vbs
del CreateShortcut.vbs
 
If this helps you out, please leave a comment below letting us know!

How to Get a List of Users With Password Never Expires and Disable the Option for All Users

Your organization may be facing a common compliance problem, having many of your users with the "Password Never Expires" option set in Active Directory. For any number of reasons, you may need to get a list of users with this option set. You may also need to disable this option for all users. Below are examples on how to list all users with this option enabled, and how to disable this option for all users:

The one-liner commands below can be ran from the command prompt as administrator.

To list all Active Directory users with the "Password Never Expires" option set:

dsquery *  -filter "(&(objectCategory=person)(userAccountControl:1.2.840.113556.1.4.803:=65536))" -limit 0

 

To turn off "Password Never Expires" for all Active Directory users:

dsquery *  -filter "(&(objectCategory=person)(userAccountControl:1.2.840.113556.1.4.803:=65536))" -limit 0 | dsmod user -pwdneverexpires no

 

**These commands may span multiple lines on your screen, but they are actually one line, and are designed to be ran as one command

How to Restart Group Policy Client Service

If you need to restart the Group Policy Client service, you will find that it's grayed out in the services list, and if you try to restart it from the command line, you get the error "System error 5 has occurred. Access is Denied". The reason you see this behavior is the Group Policy Client service needs System account permissions to be managed. If you want to restart it, you have to restart it as the System account.

You'll need the utility psexec.exe from the Sysinternals Suite. Once you download and extract it, open a command prompt (make sure to Run as Administrator) and change to the directory where psexec.exe is located. Run the following commands:

psexec.exe -s -i cmd.exe

This will launch an interactive window as the system account running cmd.exe. From this window, you can run commands as the system account, instead of your normal user account. Now run this command to stop and start the Group Policy Client service:

net stop gpsvc

net start gpsvc

How to Restart Group Policy Client Service

How to Get Public External IP From Command Line - Windows

If you are trying to find out what your public external IP address is, you typically go to a web site specifically for this purpose, such as ipchicken.com or whatsmyip.org. Finding your public IP from the command line is just as easy. Simply enter the following command into a command prompt window:

nslookup myip.opendns.com resolver1.opendns.com

This command will resolve the name myip.opendns.com using the dns server resolver1.opendns.com. If you try to resolve myip.opendns.com using any other name server, you will find it doesn't resolve. When you query it using resolver1.opendns.com, it will return the public IP that you sent the request from, which is your public IP. So thanks to OpenDNS for making this feature available to us!

This command will also work on Linux or Mac. For Linux, there is an even easier command to remember, see here

How to Use Awk to Remove the First Line of a File

Awk is a versatile pattern scanning application, installed by default on most Linux distributions. If you have Ubuntu, Debian, or openSUSE, it's already installed. You can use Awk to easily remove the first line or row of a text file. To do so, just run the following one-liner command. Make sure to specify the source file, and the file you want the processed data to go to:

awk '{if(NR>1)print}' source_file.txt > processed_file.txt

 

How to Remove Commas Inside of Quotes From a CSV or Text File on Linux with Awk

Let's say you have a CSV file that you would like to open in Excel or LibreOffice Calc to analzye. You find out the formatting is broken because there is a column that has quoted text with a comma in it:

Name, Country, City

Jason, US, Memphis

David, US, Little Rock

"Karam, Sage", US, Nazareth

As you can see, this may cause a problem when importing the file into your spreadsheet application as the 4th row has 4 commas, and the others only have 3.

This is easy to fix using Awk. Simply run this one-liner command, specifying your source file, and then a new file to output it to:

awk -F'"' -v OFS='' '{ for (i=2; i<=NF; i+=2) gsub(",", "", $i) } 1' source_file.csv >processed_file.csv

How to find a Program Version on List of Computers Using wmic and psexec

Using wmic, you can easily get a list of most of the applications installed on a computer:

wmic Product get Name

How to find a Program Version on List of Computers Using wmic and psexec1

To list the name & version of a program that you know part of the name to, run the following:

wmic product where "Name like '%Java%'" get Name, Version

To list the name & version of a program that you know the full name of, run the following:

wmic product where "Name='Java 7 Update 60'" get Name, Version

How to find a Program Version on List of Computers Using wmic and psexec 2

 

 

Now that you know how to get a program name & version using the command line, now you just need to learn how to run the command on a group of computers. There are two ways to do this, both involving psexec, a utility from the sysinternals suite.

Windows - How to Measure Time or Length of Command or Executable

There are serveral reasons you would want to see how long it takes to execute a command, or run a program. Perhaps you are benchmarking a command line application you made, or want to see how long it takes to run the same command on different computers. Whatever your reason, here are 3 ways to measure the time it takes to execute a command or application on Windows. In my example, I'm seeing how long it takes to perform a DNS query (you can replace the underlined portion with the command you want to time):

1. In PowerShell, run the following command:

Measure-Command {nslookup itswapshop.com}

 

2. Here is a one-liner you can run from the command prompt:

cmd /v:on /c "echo !time! & nslookup itswapshop.com & echo !time!"

 

3. Save this at a batch file, and then run it:

echo %time%

nslookup itswapshop.com

echo %time%

 

Here is a screenshot of each method:

Windows - How to Measure Time or Length of Command or Executable

How to Find Listening Ports on Windows from Command Line

Any version of Windows, including XP, Vista, 7, Server 2003, Server 2008, and Server 2012, you can use the netstat command to find listening ports on a system. This helps you are troubleshooting if a service or server is actually running on a system. Simply run the following command to find all listening ports on a system:

netstat -an | find /i "listening"

Find Listening Ports - Windows

Linux - History Command Tutorial - How to Run Previous Commands and Clear History

The history command is very useful for people who use the command line. If you haven't used it before, now is a great time to make it a habit, as it can save you time.

The history command by itself will provide a numbered list of previously executed commands:

history - tutorial run previous commands 1

To execute one of the previously ran commands, use the exclamation mark followed by the number of the command (no spaces):

!3

history - tutorial run previous commands 2

To clear the history, run the following command:

history -c

history - tutorial run previous commands 3

To simply run the last ran command again, run the following command:

!!

Hope this helps some of you become more proficient in the command line!

Pages

Subscribe to Command Line