PowerShell
Tipps zur Verwendung der Microsoft PowerShell, nĂĽtzliche Befehle und Skripte
-
💡 PowerShell Tip – Find a Windows Firewall rule by port
If you know the port but not the rule name, you can resolve the rule via the port filter: Useful for troubleshooting when an application opens a port but the corresponding firewall rule name is unknown.
-
đź’ˇHow to Extract Specific Lines from a File: sed, awk, head/tail, grep, and with powershell
In the command line, it’s often necessary to extract only specific lines from a text file – whether it’s a single line or a range of lines. Fortunately, Bash offers several powerful tools for this. In this blog post, I’ll show you the most common methods using sed, awk, head/tail, and grep. Additionally, I will demonstrate an option when using PowerShell. Extracting Lines with sed sed (Stream EDitor) is a classic for text manipulation in the shell and is excellent for selectively outputting line ranges.Output a Single Line (e.g., Line 102)To output a specific line, such as line 102, you use the p command (print) in combination with the -n…
-
đź’ˇPowershell Tip – Search directories and subdirectories for files with specific content
I’d like to share a small PowerShell script that scans all subdirectories for specific files (for example, *.yaml and *.yml) and searches them for a specific string (e.g., “ansible.builtin.template”). The result is returned with the location (name and path of the file, and line number of the search string in the file). What it does: Sometimes I want to save my results to a file and explore them further. To save the results to a CSV file, adapt the script as follows:
-
đź’ˇPowershell Tip – Find all files larger than X bytes in a directory structure
In order to output all files larger than a certain size on Windows systems, you do not have to use the explorer search. A Powershell script does this very efficiently. The result is shown in a gridview window: If the last part | Out-GridView is omitted, the output in the console is required.
-
Umbenennung von Dateien per powershell
In Linux Umgebungen haben Administratoren fĂĽr Logdateien von WebSphere Application Server, Tomcat und andere Middleware Produkte meist Skripte, um die Protokolle täglich oder wöchentlich umzubenennen (per crontab). Unter Windows habe ich auf die schnelle keine vernĂĽnftige Scriptlösung gefunden. Meine Lösung stelle ich in meinem Azure DevOps Projekt pwshLogSwitch zur VerfĂĽgung. Das kleine Script erwartet mindestens ein Startverzeichnis als Parameter. Alle Dateien in diesem Verzeichnis und darunter werden umbenannt, solange die Kriterien passen. Die Auswahlkriterien werden mit den Parametern -Filter -IncludePattern -ExcludePattern definiert und entsprechen den gleichnamigen Parametern von Get-ChildItem. Es können auch reguläre AusdrĂĽcke und Wildcards ĂĽbergeben werden. Als Verzeichnis können mehrere Verzeichnisnamen per : getrennt ĂĽbergeben werden. IncludePattern und…
-
đź’ˇPowershell Tip: Tail und Head vereint
Ausgabe der letzten und der ersten n-Zeilen in einemBefehl In Unix/Linux Derivaten sind die Befehle tail und Head oft benutzte Werkzeuge zur Ausgabe der ersten/letzten Zeilen einer Datei oder der Ergebnisse eines Befehls. Direkt kombinieren kann man sie nicht. Hier sind sed/awk und andere Werkzeuge und Lösungen gefordert. Powershell bietet eine sehr effiziente Lösung. Der erste Ansatz wäre die Ausgabe mit Select -Last und Select -First zu behandeln. Das geht aber nicht in einem Befehl (oder nur mit einer zu schreibenden Funktion). Damit haben wir das gleiche Problem wie mit head und tail in Unix. Per Get-ChildItem und einem auf die Ausgabe angewendetem Sort Befehl erhält man eine Liste. Diese…
-
đź’ˇPowerShell Tip for Large Files – The Get-Content -Tail and -Wait parameters
In PowerShell, you can display the last *n* lines of large text or ASCII files using the `-Tail` parameter of the `Get-Content` cmdlet: In Powershell, you can display the last *n* lines of large tyt or ASCII files using the -Tail parameter of the Get-Content cmdlet: Since PowerShell version 3, Get-Content (or its alias gc) includes the -Tail parameter. Unfortunately, there is no built-in equivalent to the Unix tail -f command for continuous updates. PowerShell: Get-Content with -Wait PowerShell’s Get-Content cmdlet supports the -Wait parameter, which continuously monitors a file for new content: This will display new lines as they are appended to the file. PowerShell with filtering (advanced usage)…
-
đź’ˇPowershell Tip: Equivalent to Unix ‘ls -ltr | tail’
In PowerShell, the output of the latest n files can be obtained with this command: In this example, the files in the current directory are output using gci, sorted by last write access using sort (newest last), and then only the last 60 are output using select last 60. Related posts
-
PowerShell Drive alias:
PowerShell nutzt das Konzept der Laufwerke (=PowerShell Drives) sehr ausgiebig. So werden die Umgebungsvariablen in env: verwaltet, die Windows Registry aber auch die “normalen” Laufwerke wie “C:”. Auch Aliasse fĂĽr Kommandos sind ein PowerShell Drive sind. Get-ChildItem alias:
-
Mehrfache Einträge in großen Konfigurationsdateien oder Protokollen finden (Windows Powershell Lösung)
Im Post Mehrfache Einträge in groĂźen Konfigurationsdateien oder Protokollen finden(Linux/Unix) habe ich gezeigt, wie mehrfach vorkommende Zeilen in Textdateien gefunden und automatisch entfernt werden können. Windows hat im Zuge der Powershell ein mächtiges Werkzeug, dass dies auch kann. (Seit dem Windows 10 1709 Update kann man natĂĽrlich auch einfach die eingebaute bash nehmen….) Zur Demonstration verwende ich eine Datei duplicates.txt: Get-Content -Path duplicates.txt Zeile kommt nur einmal vor Zeile kommt zweimal vor Zeile kommt dreimal vor Zeile kommt zweimal vor Zeile kommt dreimal vor Zeile kommt auch nur einmal vor Zeile kommt dreimal vor Mehrfache vorkommende identische Zeilen und deren Anzahl findet und zählt man wie mit Group-Object Get-Content -Path…