Powershell
A collection of some powershell-snippets I found useful, can be found under:
History#
You can get the history of your current powershell session with Get-History
.
To access older history, you can use the file stored under (Get-PSReadLineOption).HistorySavePath
.
In this file a maximum of (Get-PSReadLineOption).MaximumHistoryCount
commands is stored.
With fzf
you can access this list comfortably:
function FuncFuzzySearchHistory(){
$cmd = $(Get-Content $((Get-PSReadLineOption).HistorySavePath) | fzf)
$cmd
Invoke-Expression $cmd
}
Prompt#
To change the behaviour of the powershell prompt before running the command, alter the prompt
-function.
## This sets the window-title with the last three dir of the path
function prompt(){
$three = (Get-Location).Path -split '\\' | Select-Object -Last 3
$path = $three[0] + "\" + $three[1] + "\" + $three[2]
$host.ui.RawUI.WindowTitle = $path
"$(Get-Location)> "
}
Constructing an alias with arguments#
You can use the builtin-args variable for that.
function DotFilesRepo(){
$base = "git.exe --git-dir=$env:USERPROFILE\repos\dotfiles\ --work-tree=$env:USERPROFILE"
$cmd = "$base $args" # The $args-variable is builtin and gets all arguments. Discoverd by accident.
Invoke-Expression -Command $cmd
}
function DotFilesReposStatus(){
dfr status
}
Set-Alias -Name dfr -Value DotFilesRepo
Set-Alias -Name dfrs -Value DotFilesReposStatus
Aliases#
When constructing an alias with the corresponding function, keep in mind that powershell
is not case-sensitive.
That means, Set-Alias -Name wiki -Value Wiki
would set alias that overwrites the function.
My approach to this is to add the appropriate verb to the function name, e.g. Set-Alias -Name wiki -Value EnterWiki
.
Params#
If you want to use flags to activate a function, you can use a [switch]
.
param(
[switch]$SetTaskScheduler = $False,
)
Write-Host $SetTaskScheduler
if ($SetTaskScheduler){
Write-Host "Setting the task"
}
Task Scheduler#
Credentials Object#
If you want to safely pass username and password, you can create a Credentials object
$username = "admin1234"
$password = "mysupersecurepassword"
[securestring]$secStringPassword = ConvertTo-SecureString $password -AsPlainText -Force
[pscredential]$cred = New-Object System.Management.Automation.PSCredential ($username, $secStringPassword)
Powershell Data File#
Instead of using json
, yaml
or another well-known config-file format for your script, you can go the native powershell way with psd1
-file.
A simple one-dimensional config (e.g. config.psd1
) would look like this:
@{
Username = "ftp-user"
Password = "supersecretftppassword"
Exclude = @("PT*0730*.DAT")
}
In your script you would import the config-file and access the variable like properties:
$config = Import-PowershellDataFile -Path ".\config.psd1"
$config.Username
$config.Password
$config.Exclude
Tips and Tricks#
Get all line of go#
Get-ChildItem -Recurse -Include '*.go' | Get-Content | Measure-Object -Line