Inhaltsverzeichnis
Filesystem
List of files in a folder
(ls -r *.txt).fullname
Delete files with pattern
Get-ChildItem *.code -recurse | foreach { Remove-Item -Path $_.FullName }
Files
Check if a file exist
Test-Path $PROFILE
Searching in Files
Get-ChildItem -Recurse | Select-String "dummy" -List | Select Path
Get-ChildItem -Recurse *.sql | Select-String "create .*_tab_" | Select-Object -Unique Path
Environment
Show env variables | gci env:* | sort-object nam |
Show env variables with values | gci env:* |
Show env variables with name pattern | gci env: | where name -like '*HOME' |
Processes
Show processes using a specific port
Network
Pipeline
Parse out fromcommand
$REPOSITORY=<URL of Repository> git branch -r | ForEach-Object { Write-Output "git clone -b $_ $REPOSITORY $_" } | Out-File -FilePath .\clone-all-branches
Permissions
Show current policy
Get-ExecutionPolicy
Allow custom scripts to execute
Set-ExecutionPolicy -Scope CurrentUser unrestricted
Profiles
Different PowerShell profiles
Description | Path |
Current User, Current Host – console | $Home[My ]DocumentsWindowsPowerShellProfile.ps1 |
Current User, All Hosts | $Home[My ]DocumentsProfile.ps1 |
All Users, Current Host – console | $PsHomeMicrosoft.PowerShell_profile.ps1 |
All Users, All Hosts | $PsHomeProfile.ps1 |
Current user, Current Host – ISE | $Home[My ]DocumentsWindowsPowerShellMicrosoft.P owerShellISE_profile.ps1 |
All users, Current Host – ISE | $PsHomeMicrosoft.PowerShellISE_profile.ps1 |
Show path for all profiles
$PROFILE | Format-List * -Force
Create a new profile
New-Item $PROFILE.CurrentUserAllHosts -ItemType file -Force
Customizing
Theming
Install Posh-Git and Oh-My-Posh.
Install-Module posh-git -Scope CurrentUser Install-Module oh-my-posh -Scope CurrentUser
Install-Module -Name PSReadLine -AllowPrerelease -Scope CurrentUser -Force -SkipPublisherCheck
Then run “notepad $PROFILE” and add these lines to the end:
Import-Module posh-git Import-Module oh-my-posh Set-Theme Paradox
Set a custom theme
Import-Module posh-git Import-Module oh-my-posh Set-Theme Paradox
Show current theme settings
$ThemeSettings
$ThemeSettings.CurrentThemeLocation
Customize Prompt
Show current Path
function prompt { "PS " + $(get-location) + "> " }
Randor Color
function prompt { $random = new-object random $color=[System.ConsoleColor]$random.next(1,16) Write-Host ("PS " + $(get-location) +">") -nonewline -foregroundcolor $color return " " }
Display current time at the end of prompt line (this will mess up you console buffer)
function prompt { $oldposition = $host.ui.rawui.CursorPosition $Endline = $oldposition $Endline.X+=60 $host.ui.rawui.CursorPosition = $Endline Write-Host $(get-date).Tostring("yyyy-MM-dd HH:mm:ss") $host.ui.rawui.CursorPosition = $oldposition Write-Host ("PS " + $(get-location) +">") -nonewline -foregroundcolor Magenta return " " }
Show current user, host, current line number
$global:CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent() function prompt { $host.ui.rawui.WindowTitle = $CurrentUser.Name + " " + $Host.Name + " " + $Host.Version + " Line: " + $host.UI.RawUI.CursorPosition.Y Write-Host ("PS " + $(get-location) +">") -nonewline -foregroundcolor Magenta return " " }
Security
Signing Powershell Scripts
From here
Creating a self-signed Certificate
❯ New-SelfSignedCertificate -DnsName user@via-internet.de -CertStoreLocation Cert:\CurrentUser\My\ -Type CodeSigning PSParentPath: Microsoft.PowerShell.Security\Certificate::CurrentUser\My Thumbprint Subject EnhancedKeyUsageList ---------- ------- -------------------- 4AED871E6DB5FF3E85EB1625C5369DBDB3E120FD CN=user@via-interne… Codesignatur
Start certmgr.exe
❯ certmgr.msc

Copy created Certificate

Import the Certifcate in Trusted Root Certification Autorities and Trusted Publisher
Die englischen Namnen der Ordner sind:
- Trusted Root Certification Authorities
- Trusted Publishers.


Exportieren


Importieren

Powershell Skript signieren
❯ Set-AuthenticodeSignature -FilePath demo.ps1 -Certificate (Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert) Directory: D:\TMP SignerCertificate Status StatusMessage Path ----------------- ------ ------------- ---- 4AED871E6DB5FF3E85EB1625C5369DBDB3E120FD Valid Signature verified. demo.ps1
Final Check
❯ Get-AuthenticodeSignature .\demo.ps1 Directory: D:\CLOUD\Environments\Keycloak\Keycloak\12.0.1\bin SignerCertificate Status StatusMessage Path ----------------- ------ ------------- --- 4AED871E6DB5FF3E85EB1625C5369DBDB3E120FD Valid Signature verified. demo.ps1
From Bash to Powershell
which
(get-command FILE.EXE).Path
Set-Alias where Get-Command where FILE.EXE