Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Tuesday, May 14, 2024

Get IP address, subnet, gateway and DNS server of the NICs using PowerShell

Get IP address, subnet, gateway and DNS server of the NICs using PowerShell

Refs:
PowerShell/IP/Address/gateway/DNS

Tuesday, January 09, 2024

PowerShell Quick Tip: Get alias for a cmdlet

PowerShell Quick Tip: Get alias for a cmdlet

Below shows how to get the aliases for Remove-Item cmdlet.
PS C:\prj> Get-Alias -Definition Remove-Item
CommandType     Name                   Version    Source
-----------     ----                   -------    ------
Alias           del -> Remove-Item
Alias           erase -> Remove-Item
Alias           rd -> Remove-Item
Alias           ri -> Remove-Item
Alias           rm -> Remove-Item
Alias           rmdir -> Remove-Item

PowerShell Quick Tip: Delete a folder recursively

PowerShell Quick Tip: Delete a folder recursively

PS C:\> Remove-Item -Recurse -Force .\folder_to_delete\

Or using alias

PS C:\> rm -r -fo .\folder_to_delete\

Monday, January 01, 2024

PowerShell Quick Tip: Reading an XML file

PowerShell Quick Tip: Reading an XML file

Say you have an XML data like below. To read the IP element in PowerShell, do: Tags: PowerShell, Xml

Thursday, April 27, 2023

Install PowerShell using winget

(Formerly PowerShell Core) PowerShell is a cross-platform task automation solution made up of a command-line shell, a scripting language, and a configuration management framework. PowerShell runs on Windows, Linux, and macOS.

To install PowerShell on Windows using winget, do:
PS C:\>winget install Microsoft.PowerShell
Note:
To install winget on Windows 2022 follow this link.

Wednesday, April 26, 2023

PS Quick Tip: Get Windows OS country code

PS Quick Tip: Get Windows OS country code

PS C:\>Get-ItemProperty 'HKCU:\Control Panel\International\Geo\' | select -exp Name

Sunday, February 19, 2023

PowerShell Cheat sheet

 

PowerShell Cheat Sheet

While loop

while ($true) {
    Write-Host "Hello World"
}


Append to file

New-TimeSpan -Start @(Get-Process explorer)[0].StartTime | Out-File -Append -FilePath C:\tmp\log.txt


Keywords: PowerShell Quick Tips

REF:003


Wednesday, October 05, 2022

PowerShell Quick Tip: Change screen resolution from the command line

PowerShell Quick Tip: Change screen resolution from the command line

I had a VM that I can't open Display Settings, so had to figure out how to change the screen resolution from the command line. This can also be handy if you have several machines that for some reason has to be running at specified resolution, for example, running UI automated tests.
PS C:\>Set-DisplayResolution -Width 1600 -Height 900

Tags:PowerShell, command line, screen resolution

Saturday, May 14, 2022

Disable IPv6 from the command line

Disable IPv6 from the command line

PS C:\>New-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters -Name DisabledComponents -Value 0xFF -PropertyType DWord

To verify that it was updated:
PS C:\>Get-Item HKLM:SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters

Keywowrds: Windows IPv6 disable

Sunday, December 05, 2021

PowerShell Quick Tips: Touch file

PowerShell Quick Tips: In Linux/Unix, touching a file means to create, change or modify the timestamp of a file. Windows via cmd.exe nor PowerShell.exe does not have this tool by default. To create a file do the following in PowerShell

PS C:\> echo $null > filename.txt

Thursday, July 30, 2020

Use Windows Event Log to log events from PowerShell

Windows Event Log is a convenient event store to log information. Once data is in Windows Event log it can be correlated with other information on the system. 

There two basic steps to do this:
  1. Create log location and source
  2. Write log information into that location and source

To create log location and source, do:
PS C:\> New-EventLog -LogName 8thstring -Source 8thSource

Where:
8thstring is the log location
8thSource is the source
You have to run this on an elevated PowerShell session. And if you have Windows Event Viewer open, you have to close it and re-open to see the new log. This should show up under Applications and Service Logs

To write to that log location and source, do:
PS C:\> Write-EventLog -LogName 8thstring -Source 8thSource -Message "Hello world" -EventId 0 -EntryType information

Where:
-LogName 8thstring //8thstring was created using New-EventLog above
-Source 8thsource //8thSource was also created as part of New-EventLog call above
- Message "Hello world" //Message we wanted to write 
-EventId 0 //User defined ID assigned for this message
-EntryType information // Can be (information|warning|error)

Reference(s):

Keywords:
PowerShell
Windows Eveng Log

Tuesday, December 03, 2019

PS Quick Tips: Change Primary DNS Server of a network interface

PS c:\> Set-DnsClientServerAddress -InterfaceIndex 10 -serveraddresses ("192.168.137.1")

Where:
10 - is the interface index. This can be found by running Get-NetAdpater, this will be the column named ifIndex.
("192.168.137.1") - is the DNS server address(es) you want to assign to that interface, this is a comma separated list.

Note:
This only works on Powershell 3 (or maybe later).

Reference(s):
http://4sysops.com/archives/how-to-configure-a-network-interface-card-nic-with-powershell-3/

Powershell Quick Tips: Get details of network interface

PS c:\> Get-NetAdapter | where {$_.Name -eq "Ethernet 3"} | Format-List *
Where:
Ethernet 3 - is the name of the interface

Note:
This will need Powershell 3.0 or later

Powershell Quick Tips: Generate GUID

PS c:\> [guid]::NewGuid()

Powershell Quick Tips: Running SCVMM scripts

Open Virtual Machine Manage Command Shell. From the shell type:
PS c:\> Invoke-Expression C:\Users\User01\Documents\script1.ps1

Where:
C:\Users\User01\Documents\script1.ps1 - the script to be run

PowerShell Quick Tips: Get membership of a given Windows user


Where:
timus - is the user you want to get membership of
mydomain - domain name or PC name if workgroup

Enable ClearType using PowerShell

When I remote desktop to Windows machines most of the time the fonts just looks ugly. This can be improved by enabling ClearType and I do this every time I connect to a box which I do several times a day. Below is a Powershell code that enables ClearType which I shamelessly copied from StackOverflow.

Reference(s):
http://stackoverflow.com/questions/5676768/enable-disable-cleartype-in-windows7

Disable/Enable network interface using PowerShell

Windows 10 update 1511 sometimes losses connection inside a VirtualBox(5.0.16) VM. Mostly it happens after the host machine recovers from sleep or hibernate. Workaround I have observed so far is to disable/enable the interface. This can get boring overtime, so below is the PowerShell script that I use to automate it.

Note that O is the name of my network adapter.

Hide an application from the taskbar

Hide an application (notepad in this case) from task bar using PowerShell

Reference(s):
http://stackoverflow.com/questions/7219063/win32-how-to-hide-3rd-party-windows-in-taskbar-by-hwnd

Tuesday, January 23, 2018

PS: Get service account names

PS c:\> Get-WmiObject Win32_Service -Filter "startname like '_NetBiosName_%'" | Format-Table name, startname, startmode

Formatting code in Blogger

I have been using Github gists to share code and I have no plan of abandoning it. For smaller/one liners, I sometimes use div element with ...