Just one of the things I'm learning. https://github.com/hchiam/learning
Related repo: https://github.com/hchiam/learning-bash-scripts
Once you're in PowerShell, you can run the test.ps1
file:
cd learning-powershell
./test.ps1
brew cask install powershell
pwsh
exit
I personally like to use this:
Get-History -Count 1 | Format-List -Property *; # for just the previous/last command
https://www.howtogeek.com/298244/how-to-use-your-command-history-in-windows-powershell
Get-History
Get-History | Format-List -Property * # shows execution status and start/end times
Get-History | Export-Clixml -Path c:\users\name\desktop\commands.xml # saves history to a file
Clear-History
Clear # clears PowerShell window
[console]::beep(500,2000)
(1st parameter adjusts frequency, 2nd parameter adjusts duration)
npm run build; [console]::beep(400,2000);
Get-History -Count 1 | Format-List -Property *; # you canNOT run this in a one-liner combined with the previous command, because I found it ends up printing history for the last one-liner that you entered, not for the combined one-liner that includes this history command
-
To set up custom commands, set up a profile file in one of the profile folders: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.3
-
Then add custom commands in that profile file: https://learn.microsoft.com/en-us/powershell/scripting/developer/module/how-to-write-a-powershell-script-module?view=powershell-7.3
-
And then restart powershell (if you have it already open). You can now run those custom commands!
Example custom commands test
and hist
in your $PROFILE file:
function test {
param()
Write-Output "test output"
}
function hist {
Get-History -Count 5 | Format-List -Property *;
Get-History;
}
Get-Command somecustomcommand
function somecustomcommand {
npm run clean;
npm i;
npm run build;
}
function exampleShowingCodeStillRunsEvenAfterErrorIfSeparateLines {
asdfasdfasdf;
echo "this still prints";
}
ls -r *.js | rm
or
ls -r your\path\*.js | rm
Read-Host "Hit enter to continue";
https://www.youtube.com/watch?v=gOFsACMBEac
Test-Path $PROFILE
# likely prints out False
New-Item -Path $PROFILE -Type File -Force
notepad $PROFILE
function prompt {
$path = $(Get-Location).Path
$path = $path.ToLower() -replace '^([a-z]):', '/$1' -replace '\\', '/' # unix style path
Write-Host ""
Write-Host $path -ForegroundColor Green
Write-Host "> " -NoNewLine -ForegroundColor Green
}
.$PROFILE
# and if get disabled error
Set-ExecutionPolicy RemoteSigned
# y
.$PROFILE
Get-ChildItem -Path "." -Directory
(Get-ChildItem -Path "." -Directory).Count
Get-ChildItem -Path . -Directory -Recurse | Where-Object { $_.FullName.Split('\').Count -eq ((Get-Location).Path.Split('\').Count + 2) }
(Get-ChildItem -Path . -Directory -Recurse | Where-Object { $_.FullName.Split('\').Count -eq ((Get-Location).Path.Split('\').Count + 2) }).Count
(Get-ChildItem -Path . -Recurse | Where-Object { $_.FullName.Split('\').Count -eq ((Get-Location).Path.Split('\').Count + 2) -and $_.Name -eq 'example.txt' }).Count
Get-ChildItem -Path . -Directory -Recurse | Where-Object { $_.FullName.Split('\').Count -eq ((Get-Location).Path.Split('\').Count + 4) } | Out-File -FilePath "U:\Downloads\out.txt"
# ... | Out-File -FilePath "U:\Downloads\out.txt"