findstr split by space and show 3rd word - cmd

On Windows 2012 I am running net user localusername | findstr "logon"
which returns Last logon 9/8/2017 9:27:16 AM
I only want it to return the date which will be different on each account. How do I filter this to only return 9/8/2017?
This is what I did for Windows 2016 and it works great, but Windows 2012 does not support grep or cut.
net user "{{ item }}" | grep "Last logon" | cut -d' ' -f21
which returns 9/8/2017
update
The following is working on the server thanks to #Mofi
for /F "tokens=3" %I in ('net user "test" 2^>nul ^| findstr "Last logon"') do #echo %I

It is easy enough to get the LastLogon timestamp using PowerShell. If you are on a supported Windows system, PowerShell will be available.
This can be put into a .bat file script.
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
"(Get-LocalUser -Name 'pwatson2_la').LastLogon -f 'd'"') DO (SET "LLD=%%~A")
ECHO LLD is %LLD%

update The following is working on the server thanks to #Mofi
for /F "tokens=3" %I in ('net user "test" 2^>nul ^| findstr "Last logon"') do #echo %I

Related

Adding a comma at the end of each line wmic

I am trying to get a comma seperated list of printers my computer has access to. So far I have #echo off & for /f "delims=" %i in ('wmic printer get name ^| findstr /v "Name"') do echo %i, but for some reason it's replacing the first letter of each printer with a comma. Changing the %i, in the end is slightly better, not replacing the first letter with a comma but just adding one in front of the name of the printer instead. Though this is better this is not what I'm looking for because I want to get the output.
PrinterOne,
Printer2,
Printer3,
etc,
I'm pretty sure %i, should work, or am I wrong?
Side note I've had a working solution for months running
For /F "Tokens=1,* EOL=' Delims=," %%G In ('%%SystemRoot%%\System32\wbem\WMIC.exe Printer Get Name^ /Format:CSV 2^>NUL') Do #For /F "Tokens=*" %%I In ("%%H") Do #Echo %%I,
from a batch file, but as it looks it does not work with some of the computers on our system. Could be windows related but I'm not sure.
I managed this with
powershell -Command "& { Get-Printer -ComputerName $env:COMPUTERNAME | Select-Object -ExpandProperty Name | foreach-object {$_ + ','}}"

How To Get The Active User's SID

The scope is running from the user's local computer.
You can use this to get the active user's SID.
Then you could use this with the HKU registry hive.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO GET THE NAME OF THE ACTIVELY LOGGED ON USER
FOR /F "skip=1" %%G IN ('wmic computersystem get username') DO (
SET aUSER=%%G
GOTO EXITLOOP1
)
:EXITLOOP1
ECHO %aUSER%
ECHO[
REM ECHO TRIM THE USERNAME
SET tUSER=%aUSER:~4%
REM ECHO %tUSER%
ECHO[
ECHO GET SID FOR USER: %tUSER%
FOR /F "usebackq skip=1" %%a IN (`WMIC USERACCOUNT WHERE NAME^='%%tUSER%%' GET SID`) DO (
SET SID=%%a
GOTO EXITLOOP2
)
:EXITLOOP2
ECHO %SID%
BTW, if you wanted to do it without a For loop, you could ask powershell to assist:
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "(%SystemRoot%\System32\whoami.exe /User /Fo CSV | ConvertFrom-Csv).SID"
Or even without whoami.exe:
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "Add-Type -AssemblyName System.DirectoryServices.AccountManagement;$([System.DirectoryServices.AccountManagement.UserPrincipal]::Current).SID.Value"
In a cmd.exe console the following command can be used.
powershell -NoLogo -NoProfile -Command ^
"Add-Type -AssemblyName System.DirectoryServices.AccountManagement;" ^
"([System.DirectoryServices.AccountManagement.UserPrincipal]::Current).Sid.AccountDomainSid.Value"
To get the result into a variable, use a FOR loop. (Yeah, I know, it's crazy, right?)
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
"Add-Type -AssemblyName System.DirectoryServices.AccountManagement;" ^
"([System.DirectoryServices.AccountManagement.UserPrincipal]::Current).Sid.AccountDomainSid.Value"') DO (
SET "USER_SID=%%~A"
)
ECHO USER_SID is set to %USER_SID%
There are many other things that can be accessed in this way.
powershell -NoLogo -NoProfile -Command ^
"Add-Type -AssemblyName System.DirectoryServices.AccountManagement;" ^
"[System.DirectoryServices.AccountManagement.UserPrincipal]::Current |" ^
"Format-List * -Force"
As you've raised a question, and in it decided to post some code, I'll offer a quicker and more simple alternative, regardless of whether you decide to post yours as a solution:
From cmd:
For /F Tokens^=3^ Delims^=^" %G In ('%SystemRoot%\System32\whoami.exe /User /Fo CSV /NH') Do #Echo %G
From a batch-file:
#For /F Tokens^=3^ Delims^=^" %%G In ('%SystemRoot%\System32\whoami.exe /User /Fo CSV /NH') Do #Echo %%G

Script to get Windows version and send it by email

I want to monitor version of Windows on all computers in a domain. So I want to create a .bat file that get the local Windows version and if possible send it via email using telnet command.
I never wrote a script in Windows. So it's difficult for me to start in this area. So any help will be appreciated.
for /f "skip=3 delims=\" %%A in ('net view ^| findstr /v /C:"The command completed successfully"') do Echo %%A
Will give you a list of computernames. Put it in a file.
for /f "skip=3 delims=\" %%A in ('net view ^| findstr /v /C:"The command completed successfully"') do Echo %%A >> Computername.txt
Then type to do turned on computers
wmic /node:#"Computername.txt" os get version /format:csv

Collect each service's display name and status

I want to get list of services with their display name and their status.
This is what I have tried:
for /f "tokens=2" %s in ('SC query state^= all ^| find "DISPLAY_NAME"') do #(for /f "tokens=4" %t in ('SC query %s ^| find "STATE"') do #echo %s is %t)
But this returns only limited services such as disk, etc.
This is a perfect task for the built-in WMI command line executable, WMIC.exe.
From the cmd.exe prompt:
For /F "Skip=1 Delims=" %A In ('"WMIC Service Get DisplayName, Name, State"') Do #For /F "Delims=" %B In ("%A") Do #Echo(%B
From a batch file:
#For /F "Skip=1 Delims=" %%A In ('"WMIC Service Get DisplayName, Name, State"'
) Do #For /F "Delims=" %%B In ("%%A") Do #Echo(%%B
#Pause
Try getting help from powershell directly in your batch file, like this for example (save as .bat and run it). In the example i type it to the screen and give you ALL services, RUNNING ONLY, and STOPPED ONLY but you could do basically what you want with the content of that txt file (search it with a loop, save a part to a variable, etc.).
#echo off&cls
pushd %~dp0
echo.
echo List ALL services
pause
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "powershell Get-Service | Out-File services.txt"
type services.txt
echo.
echo List services that are RUNNING only
pause
find "Running" services.txt
echo.
echo List services that are STOPPED only
pause
find "Stopped" services.txt
pause
:cleanup
del /f services.txt

Find if a computer is using DHCP or Static through CMD?

I am looking for a cmd command to return if the currently connected NIC is using a static or DHCP address. DHCP: YES or NO is acceptable.
I think ipconfig might be an option, but I need a way to sort out the DHCP field as well as make sure the field I am pulling is from the correct adapter.
I found this code which will pull out the IP Address, But I have not be able to adjust the code to pull the DHCP status.
for /f "delims=" %a in ('ipconfig ^| findstr [0-9].\.') do #echo %a | findstr "Address"
I tried
for /f "delims=" %a in ('ipconfig ^| findstr [0-9].\.') do #echo %a | findstr "DHCP Enabled"
and
for /f "delims=" %a in ('ipconfig ^| findstr [0-9].\.') do #echo %a | findstr "DHCP"
but they return nothing. I think im on the right track, I'm just not sure.
for /f "tokens=2 delims=:" %a in ('ipconfig /all ^|find "DHCP Enabled"') do echo DHCP:%a
Note: dependent on locale (language)
Note: will give you a line for every adapter (without telling you, which adapter)

Resources