MyWinCmds

From TheBestLinux.com
Jump to navigation Jump to search

WINDOWS COMMAND-LINE COMMANDS & TOOLS

PowerShell Commands

Windows Powershell Commands & Examples - All Windows Versions

Here are a bunch of Powershell commands that work in most, if not all, versions of Windows. Of course, the newer the version of Windows, the more command there are available, and the way they are used sometimes also has changed. This is where the Windows built-in "Get-Help" commandlet comes in handy. It is similar to the built-in "man",(short for "MANUAL"), as it's available via the command line for most commands.

Windows Version On the topic of what Powershell commands are available on which versions of Windows, this first section goes over the different methods of finding out exactaly which version of Windows you are dealing with.

  • Get-ComputerInfo
    • This get's the Windows version as seen by HAL, the Hardware Abstraction Layer:
PS C:\Users\jamie.DAWGLAND> Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer

WindowsProductName             WindowsVersion OsHardwareAbstractionLayer
------------------             -------------- --------------------------
Windows Server 2022 Datacenter 2009           10.0.20348.558


PS C:\Users\jamie.DAWGLAND>



  • Get-ItemProperty
    • This command get's the value of the current version in the Windows Registry:
PS C:\Users\jamie.DAWGLAND> (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId
2009
PS C:\Users\jamie.DAWGLAND>



  • systeminfo
    • The "systeminfo" command retrieves a ton of information about the OS and hardward. In this example, I use the "findstr" command to pipe the output of the systeminfo command into findstr to only retreive lines that contain the string "OS", as seen here:
PS C:\Users\jamie.DAWGLAND> systeminfo | findstr OS       
OS Name:                   Microsoft Windows Server 2022 Datacenter
OS Version:                10.0.20348 N/A Build 20348
OS Manufacturer:           Microsoft Corporation
OS Configuration:          Primary Domain Controller
OS Build Type:             Multiprocessor Free
BIOS Version:              VMware, Inc. VMW201.00V.20192059.B64.2207280713, 7/28/2022
PS C:\Users\jamie.DAWGLAND>



  • [System.Environment]::OSVersion.Version
    • Pull the Windows version from the system's environtment variables:
PS C:\Users\jamie.DAWGLAND> [System.Environment]::OSVersion.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      20348  0

PS C:\Users\jamie.DAWGLAND>



  • Get-ComputerInfo -Property Windows*
    • Using the * wild-card to grab the actually pertinent information with one command:
PS C:\Users\jamie.DAWGLAND> Get-ComputerInfo -Property Windows*


WindowsBuildLabEx              : 20348.1.amd64fre.fe_release.210507-1500        
WindowsCurrentVersion          : 6.3
WindowsEditionId               : ServerDatacenter
WindowsInstallationType        : Server
WindowsInstallDateFromRegistry : 1/19/2023 1:14:37 AM
WindowsProductId               : 00454-60000-00001-AA331
WindowsProductName             : Windows Server 2022 Datacenter
WindowsRegisteredOrganization  :
WindowsRegisteredOwner         : Windows User
WindowsSystemRoot              : C:\Windows
WindowsVersion                 : 2009



PS C:\Users\jamie.DAWGLAND>



  • (Get-CimInstance Win32_OperatingSystem).version
    • Last, but not least:
PS C:\Users\jamie.DAWGLAND> (Get-CimInstance Win32_OperatingSystem).version
10.0.20348
PS C:\Users\jamie.DAWGLAND>



Reading Windows Registry Values to Get System Info & to Use in Scripts!

  • Reading registry values from the commandline(CMD):
    • Syntax:
REG QUERY [ROOT\]RegKey /v ValueName [/s] 
REG QUERY [ROOT\]RegKey /ve --This returns the (default) value
Where RegKey is the key which needs to be searched for in the registry.
  • Example
@echo off 
REG QUERY HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows\

The above command will query all the keys and their respective values under the registry key
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows\

This location in the registry has some key information about the windows system such as the System Directory location.

  • Example output from above "REG QUERY" command:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows
   Directory REG_EXPAND_SZ %SystemRoot%
   SystemDirectory REG_EXPAND_SZ %SystemRoot%\system32
   NoInteractiveServices REG_DWORD 0x1
   CSDBuildNumber REG_DWORD 0x4000
   ShellErrorMode REG_DWORD 0x1
   ComponentizedBuild REG_DWORD 0x1
   CSDVersion REG_DWORD 0x0
   ErrorMode REG_DWORD 0x0
   CSDReleaseType REG_DWORD 0x0
   ShutdownTime REG_BINARY 3AFEF5D05D46D101


Enable Remote Desktop Protocol(RDP) from the Command Line(CMD)

Here are the badic commnds to enable RDP(Remote Desktop Protocol) from the command line(CMD)

Windows Server - Specific

This one-liner will give you the EXACT version of Windows currently running:

(Get-WmiObject -class Win32_OperatingSystem).Caption



Query Detailed System Information - Here is an example of how to obtain detailed system information about the server you are currently working on:


SConfig						Start Server Configuration text-mode command-line tool for Server Core without GUI
Get-History					Gets history of current session
(Get-PSReadlineOption).HistorySavePath		This gives you the path where all of the history is saved.
cat (Get-PSReadlineOption).HistorySavePath	Spit out all history!
cat (Get-PSReadlineOption).HistorySavePath | select -Last 10 --> get the last 10 lines
Get-Content (Get-PSReadlineOption).HistorySavePath > C:\PowerShellHistory.txt  --> Copy all history to file
notepad (Get-PSReadlineOption).HistorySavePath	Open all history in Notepad GUI
Select-String "<search pattern>" (Get-PSReadlineOption).HistorySavePath  --> Search All History
		Same as above but without the details/line numbers/etc:
Get-Content (Get-PSReadlineOption).HistorySavePath | Select-String "<search pattern>" 

Basic Command:
cat (Get-PSReadlineOption).HistorySavePath

SSH Server & Client Setup on Windows Using PowerShell commands:
From URL:
https://techcommunity.microsoft.com/t5/itops-talk-blog/installing-and-configuring-openssh-on-windows-server-2019/ba-p/309540

Add-WindowsCapability -Online -Name OpenSSH.ServerJamie Rubinstein | Jamie@dawgland.com | https://www.dawgland.com 19:07, 8 May 2023 (UTC)0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.ClientJamie Rubinstein | Jamie@dawgland.com | https://www.dawgland.com 19:07, 8 May 2023 (UTC)0.0.1.0

If you’re intending to use key based, rather than password based, authentication, you should also run the following command on the server to install an OpenSSH related PowerShell module that includes tools to help you configure that functionality:

Install-Module -Force OpenSSHUtils -Scope AllUsers

To install NANO text editor, as per URL this is based on (I prefer the VI/VIM text editor, which is included with the GitHub command-line client, which also provides basic BASH shell functionality and tools and support for the built-in Windows OpenSSH Server & Clients!

Other Miscellaneous Commands:

mstsc.exe Start MS Remote Desktop Client (RDP) sysdm.cpl Start Rename Windows Hostname & Join Domain/Workgroup & Device Manager & RDP Enable. appwiz.cpl Start Add/Remove Programs control netconnections Open and edit network connections control update Start Windows Updates The BIG List of Windows Commands https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands

Windows Server LICENSE MANAGER Commands:

Check Activation Status slmgr /xpr View Current License Info slmgr /dli View Detailed License Info slmgr /dlv Reset Activation Timers slmgr /rearm Remove Windows Product Key slmgr /cpky Activate Windows Online slmgr /ato Change Windows Product Key slmgr /ipk 12345-12345-12345-12345-1234 Change Remote Product Key slmgr /ipk Win10Vm10 jamie MyPasswd 12345-12345-12345-12345-12345 Deactivate Windows slmgr /upk DETAILED SLMGR USAGE WITH ALL AVAILABLE SWITCHES & OPTIONS: slmgr [MachineName [username [password]]] [option] Slmgr Command Options Item Explanation MachineName The machine to administer. Defaults to the local machine if omitted. username The username of an administrator account on the remote machine. password The password for username. /ato Activate Windows license and product key against Microsoft's server. /atp Confirmation_ID Activate the product with a user-provided Confirmation_ID. /cdns Disable DNS publishing by the KMS host. /ckhc Disable KMS host caching. /ckms Clear the name of KMS server used to default and port to default. /cpky Delete the Windows product key from Windows Registry. /cpri Set the KMS priority to low. /dli Display the current license information with activation status and partial product key. /dlv Display additional license information. Similar to /dli but more detailed. /dti Display Installation ID for offline activation. /ipk key Change the Windows product key. Replaces the current product key if present. /ilc file Install a license file. /rilc Reinstall system license files. /rearm Reset the evaluation period/licensing status and activation state of the computer. Use /rearm-app to specify an app, or /rearm-sku for a specific sku. /skms Set the Volume Licensing KMS server and/or the port used for KMS activation. /skhc Enable KMS host caching (enabled by default). This blocks the use of DNS priority and weight after the initial discovery of a working KMS host. /sai interval Sets the interval in minutes for unactivated clients to attempt KMS connection. /spri Set the KMS priority to normal (default). /sprt port Set the port on which the KMS host listens for client activation requests (default TCP port is 1688). /sdns Enable DNS publishing by the KMS host (default). /upk Uninstall the currently installed Windows product key and return the license status back to a trial state. /xpr Show the expiry date of the current license or indicate whether activation is permanent. The MachineName option can't be used across platforms. For example, you can't manage Windows activation on a Windows 7 computer from a Windows Vista computer. Slmgr Uses Software Licensing Management Tool (slmgr) is a VBS file in Windows against which you can run commands to perform advanced Windows product activation tasks. Slmgr.vbs is used only for the Windows operating system. Ospp.vbs manages volume licensing for Microsoft Office products.

MSC - Microsoft Management Console Commands

* adsiedit.msc * azman.msc * certlm.msc * certmgr.msc * certsrv.msc * certtmpl.msc * comexp.msc * compmgmt.msc * devmgmt.msc * DevModeRunAsUserConfig.msc * dfsmgmt.msc * diskmgmt.msc * dnsmgmt.msc * domain.msc * dsa.msc * dssite.msc * eventvwr.msc * fsmgmt.msc * fsrm.msc * gpedit.msc * gpmc.msc * gpme.msc * gptedit.msc * lsdiag.msc * lusrmgr.msc * nfsmgmt.msc * ocsp.msc * perfmon.msc * pkiview.msc * printmanagement.msc * rrasmgmt.msc * rsop.msc * secpol.msc * services.msc * taskschd.msc * tpm.msc * tsgateway.msc * WF.msc * WmiMgmt.msc

More Useful Command Lists From Other Sites

* https://www.lifewire.com/windows-run-command-guide-3401937

Batch File Scripting

* Reading from the Registry * Enable Remote Desktop Protoco(RDP) from the command line