How to Use Subexpressions Within Strings in Powershell

echo "the length is $($list.length)"
echo "today is $(Get-Date)"

EXAMPLE_1

  • this is used for a logger to stdout when printing a many jobs at once, I want to see the progres (e.g job# 3/15)
$i = 1
foreach ($jobId in $jobIds) {
if($jobIds.length -gt 1) {
Write-Host "-- Dwarf Counts: Job #${i}/$($jobIds.length) --" -backgroundcolor DarkYellow-foregroundcolor Magenta
$i++
Pause}
}

EXAMPLE_2

  • I copy the $url and $title to the clipboard of a page with the help of a Chrome expression
  • Then I call this tiny function which
  • creates a bookmarked file where I can come later to add notes if needed
  • importantly, it automatically populates playlist.md which you can see at read_list | ✒ Pavol Kutaj ✒
  • the significant line is "* $(read-host 'notes')" >> $playlist_path because it concatenates * with the input from terminal and appends to the path without having to need the terminal
function bookmark($url,$title) {
$title = $title -replace "\|.*"
$playlist_path = "C:\Users\Admin\Documents\workspace\work.log\pkutaj\playlist.md"
echo "`n#### $title" >> $playlist_path
echo "* $url" >> $playlist_path
"* $(read-host 'notes')" >> $playlist_path
New-Kba k -name $title -url $url -cat "bookmarks" -extract $title
}

LINKS

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com