Powershell

Profiles

Directory that contains Profiles

Get-ChildItem  ~\Documents\WindowsPowershell\

Possible output for Terminal, ISE and VS Code

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         7/29/2023  11:08 AM            172 Microsoft.PowerShellISE_profile.ps1
-a----         7/29/2023  11:12 AM           1170 Microsoft.PowerShell_profile.ps1
-a----         7/29/2023  11:08 AM            158 Microsoft.VSCode_profile.ps1

Find all profiles or currently used Profile with:

$PROFILE | Select-Object *
$PROFILE | Select-Object CurrentUserCurrentHost

Shutdown the Computer

use shutdown or stop-computer

  • /s flag for shutting down

  • /t flag delays shutdown for xx seconds (3600s = 1h, 14400s = 4h)

  • /a flag aborts a shutdown (also seems to work for forced updates and maintenance)

Restart the Computer

Restarts the Computer with Other(planned) reason

SSH Sessions with Posh

This starts a new SSH Session through Powershell to a linux computer

ssh keys

Windows/powershell does not interpret ~ use $HOME instead and backward slashes For some reason private ssh keys need to have an empty new Line at the end of the file

Permissions

If permissions on the ssh key file are not right:

Installing the Linux Support thing for Windows

Install Ubuntu WSL https://docs.microsoft.com/en-us/windows/wsl/install-win10 It can be started by typing bash in Powershell Run this, and only this as Administrator/root

Run the following as regular User so you don't need to be Administrator to use wsl this installs Ubuntu 18-04. Look here for current supported distros and manual download Links wsl

If WSL is already installed or to install any distro run

to reinstall a distribution run:

File Actions

Find, seek and destroy

Finding Files and Displaying only the Filenpath and name one per Line

Search in Files

Search for a string within a Folder and its subfolders

Delete

Delete a folder that is not empty

Recurse through a directory structure and search each files for pattern then store each match in a hash-table/dictionary with the Filename as key and a number per match.

String Actions

Base64 en- and decoding

Path actions

print PATH one entry per line

Create a range object and search through a filetree for a string that contains elements of that range

Compare aka diff two files

It has to explicitly taken the content of the files. If just two filenames are given, those are compared without regard of their actual content.

Compare Errors from to Error Log files

The following creates a table named "TableName" and add the 3 columns Date,Error1 and Error2 to it. Then it populates the table with the findings of the Files.

Chocolatey

Getting chocolatey up and running with Powershell V5

And Install it

Active Directory

On a Windows 2016 Server the RSAT Tools need to be installed, you can find it with the Powershell Commands below.

Get-ADComputer

Query for Computers with names like appsrv and display only the found names one per line

Get all Computer start contain xyz but do not start with abc

Get-ADUser

Get all Groups a User is Member of and store them in a variable

I had some problems using the Get-ADGRoupMember cmdlet with an ObjectGUID for the -Idetity Parameter. Working around that I could use the Get-ADGroup cmdlet in the following way.

Get everything about a User

Getting a list of all locked out accounts

Get Alls Users in a company

Adsisearcher

When the RSAT-AD-Powershell module is not available adsisearcher can help.

Getting all Users currently logged in to a machine/server

Add-ADGRoupMember

To add a user to an Active Directory Group user as in the Microsoft Docs

Copy Files Remotely

Copy Files from a Remote Machine

Copy Files from a to/from a remote Server by instatiating a new PSSession and then use the -ToSession/-FromSession Flag use the -Recurse Flag to copy more than one File. I did not try to copy it directly from the remote machine to the other remote machine. I assume this wouldn't work due to the second hop Problem.

Get all commands from Module

This command gets the Module containing the and then gets all the commands in that Module

Firewall Rules with Powershell

The name parameter has to be unique to the Firewall Rule, the DisplayName can be the same across several Firewall Rules, and therefore used to group Rules together.

Connection Security Rules

Connection Security Rules can be found at Control Panel > System and Security > Windows Defender Firewall > Advanced Settings > Connection Security Rules

Notes:

  • The IpSecRuleName must be unique

  • Ports can be

    • A Single Port e.g. 80

    • A range of Ports e.g. "80-443"

    • A list of Ports e.g. 80,443,8080

Select-String

Find in all Files starting with TMP the regex Pattern and print only the found Matches. Searching for content within the matching Files.

Windows Event Log

All Sources

To get a list of all available Sources, run the following. You can substitue Application with System,Setup,Forwarded Events or Security also you need Administrator Rights to access Security Logs.

The [void] prevents $SourceNames from containing the index numbers of its content.

PSReadline

Using PSReadline for better (bash-like) history. Docs

Last updated