Command Line

How to View Wireless Networks from Command Line in Windows

You aren't limited to the Windows GUI to view available wireless networks. You can use the "netsh" command to do this. This command can be use from CMD or from PowerShell. To get a list of all available wireless networks, run the command below:

netsh wlan show networks

For more details, including finding the signal strength, run this command:

netsh wlan show networks mode=bssid

How to Shutdown or Restart Windows from Command Line

You can use the "shutdown" command to shutdown or restart a Windows Operating System from the command line. This command works from a regular command prompt or from PowerShell. Here are the commands needed:

To shutdown: shutdown -s -f

To restart: shutdown -r -f

Here are a few useful notes on some of the options:

-f forces all applications to close. This is optional, but will keep hung applications from holding up the shutdown/reboot process (epecially useful for terminal services)

-t is used to wait X number of seconds before shutdown/reboot. For example, "-t 0" will start the process right away, while "-t 100" will wait 100 secons.

5 Ways to Get Windows Boot Time from Command Line

Here are 5 different ways to get the most recent boot time of a Windows workstation or server Operating System from the command line. Some of these use CMD and some use PowerShell. Each one uses a slightly different method to achieve the same result:

1.) SystemInfo

systeminfo | find /i "Boot Time"

2.) The NET command

net statistics workstation

3.) Use the PAGE File creation time (assuming default location of page file)

dir /a:h c:\pagefile.sys

4.) PowerShell Get-CimInstance

Get-CimInstance -Class Win32_OperatingSystem | Select-Object LastBootUpTime

5.) PowerShell Get-WmiObject

Get-WmiObject -class Win32_OperatingSystem | Select-Object  __SERVER,@{label='LastBootUpTime';expression={$_.ConvertToDateTime($_.LastBootUpTime)}}

If you know of any alternative methods, please leave a comment below and let our readers know!

How to List Directories Only From Command Line in Windows

If you are needing to list only the directories in a folder, this can be accomplished with the DIR command in Windows. By default, DIR will list all files and folders. To list only directories, run DIR with the following options:

dir /AD

This will give you a list of all the directories in the folder where the command was ran.

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

Pages

Subscribe to Command Line