Version 14 vs 16
Version 14 vs 16
Edits
Edits
- Edit by keithzg, Version 16
- Mar 8 2024 4:51 PM
- ·I shoulda remembered it was hyphens, not underscores
- Edit by keithzg, Version 14
- Mar 7 2024 5:59 PM
- ·Lets organize a bit more
Edit Older Version 14... | Edit Current Version 16... |
Content Changes
Content Changes
PowerShell is a hilariously verbose command environment, both in terms of every damn command and also in terms of what it spits out at you when something is wrong---at least, at the times where it doesn't just refuse to say anything at all. Unhelpfully verbose or unhelpfully taciturn seem to be its only two modes! However, PowerShell is necessary in many scenarios thanks to being more featureful and modern than Windows' vestigial `cmd.exe` environment.
So here are some important things, presuming you already know how to use normal shells on #Linux or such. And if you don't, stop reading this and learn to be comfortable there first; it's a much less user-hostile place to be, far fewer lines of red error text suddenly thrust upon you. Seriously, it's generally far less of a hassle even on Windows to just use the [[ documentation/windows/wsl/ ]]! (You can even pipe to it, ex `dir | wsl grep -i SomeFilenameIAmLookingFor`.)
== Running normal commands with quoted command or argument paths ==
When using quoted arguments or even just commands, PowerShell goes "oh hey strings" because fucking everything's an object in PowerShell and so suddenly it's thinking you're trying to do string operations when you're just trying to run a damn command with a space in it or something.
The trick is to prepend `& ` to the start of the command. Ex.
```& "C:\path\to\someplace some asshole put space\something.exe" "D:\Silly Path\"```
== Basic Equivalents ==
| Posix-y command | PowerShell equivalent | Documentation | Notes
| --------------------- | -------------------------- | ------- | ------ |
| `ls` | `ls` / `dir` / `Get-ChildItem` | [[https://ss64.com/ps/get-childitem.html| ss64]], [[ https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem | Microsoft ]] | Acts like `ls -l`, no easy equiv of just `ls`
| `grep` | `Select-String -Pattern` | [[ https://ss64.com/ps/select-string.html | ss64 ]], [[ https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Select-String | Microsoft ]]|
| `pushd` | `pushd` / `Push-Location` | [[ https://ss64.com/ps/push-location.html | ss64 ]] | Works in DOS too, less annoying than `cd` there in fact since it'll traverse filesystem boundaries |
| `cat` | `Get-Content` | [[ https://ss64.com/ps/get-content.html | ss64 ]], [[ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content | Microsoft ]]| |
| `tail -f` | `Get-Content -Wait` | | Easily chokes on bad characters and if it does so it actually won't wait, so in practice often just doesn't work |
| `top` | see [[ documentation/windows/powershell#top|below]] | see [[ documentation/windows/powershell#top|below]] | see [[ documentation/windows/powershell#top|below]] |
| `tree` | `tree /F` | [[ https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/tree | Microsoft ]] | `tree` looks fine on screen, but attempts to grep or FindStr fill the output with ????s. By default only prints folders, print files too with `/F`. **This is a basic EXE, it is NOT an alias for a PowerShell equivalent.**|
| `unzip` | `Expand-Archive` | [[https://ss64.com/ps/expand archive.html | ss64]], [[ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/expand-archive | Microsoft ]] |
| `wget`| `Invoke-WebRequest` | [[https://ss64.com/ps/invoke-webrequest.html | ss64]], [[https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest | Microsoft]]| In fact running `wget` will already get you `Invoke-WebRequest`, which [[ https://superuser.com/a/1410881 | saves the content to an object in memory ]] rather than defaulting to file output like `wget` does, making this a surprising alias. You'll need to run, say, `wget url://etc -OutFile filename.exe`. |
== Alternatives and complexities ==
=== top ===
You almost just have to grab some equivalent compiled for Windows (for example [[ https://github.com/ClementTsang/bottom/ | bottom aka btm]] is a nice cross-platform atop-like program). Install with `winget install Clement.bottom`.
The best you can really do natively in PowerShell is [[ https://superuser.com/questions/176624/linux-top-command-for-windows-powershell | crazy one-liners]] like for example:
```While(1) { $p = get-counter '\Process(*)\% Processor Time'; cls; $p.CounterSamples | sort -des CookedValue | select -f 15 | ft -a}```
=== less and cat ===
For a nicer equivalent of the `less` and `cat` commands, [[ https://github.com/sharkdp/bat | bat aka batcat ]] is the way to go. `winget install sharkdp.bat`.
== Environment variables ==
Access normal environment variables with `$env:VARIABLENAME`, ie. instead of `%USERPROFILE%` for Windows' equivalent of `$HOME` it's `$env:USERPROFILE`. Contain in curly braces to avoid ambiguity in parsing, ex. `${env:USERPROFILE}`.
== Examples ==
```
lang=powershell
# Get the current state of scheduled tasks with the string "T401" in their name
schtasks | Select-String -Pattern "T401"
# See if anything with "notepad" in the executable name are running
tasklist | Select-String -Pattern "notepad"
# Get all the version info on all the EXEs in the local directory
Get-ChildItem "*.exe" | % {$_.VersionInfo} | Select *
# Get only some info
Get-ChildItem "*.exe" | % {$_.VersionInfo} | Select OriginalFilename,FileVersionRaw
```
== External links for other problems ==
* https://stackoverflow.com/questions/53834304/how-do-i-git-clone-from-a-windows-machine-over-ssh
* [[ https://github.com/PowerShell/Win32-OpenSSH/issues/1082 | Remote SSH commands require double escaping before hitting the DefaultShell ]]
PowerShell is a hilariously verbose command environment, both in terms of every damn command and also in terms of what it spits out at you when something is wrong---at least, at the times where it doesn't just refuse to say anything at all. Unhelpfully verbose or unhelpfully taciturn seem to be its only two modes! However, PowerShell is necessary in many scenarios thanks to being more featureful and modern than Windows' vestigial `cmd.exe` environment.
So here are some important things, presuming you already know how to use normal shells on #Linux or such. And if you don't, stop reading this and learn to be comfortable there first; it's a much less user-hostile place to be, far fewer lines of red error text suddenly thrust upon you. Seriously, it's generally far less of a hassle even on Windows to just use the [[ documentation/windows/wsl/ ]]! (You can even pipe to it, ex `dir | wsl grep -i SomeFilenameIAmLookingFor`.)
== Running normal commands with quoted command or argument paths ==
When using quoted arguments or even just commands, PowerShell goes "oh hey strings" because fucking everything's an object in PowerShell and so suddenly it's thinking you're trying to do string operations when you're just trying to run a damn command with a space in it or something.
The trick is to prepend `& ` to the start of the command. Ex.
```& "C:\path\to\someplace some asshole put space\something.exe" "D:\Silly Path\"```
== Basic Equivalents ==
| Posix-y command | PowerShell equivalent | Documentation | Notes
| --------------------- | -------------------------- | ------- | ------ |
| `cat` | `Get-Content` | [[ https://ss64.com/ps/get-content.html | ss64 ]], [[ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content | Microsoft ]]| |
| `grep` | `Select-String -Pattern` | [[ https://ss64.com/ps/select-string.html | ss64 ]], [[ https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Select-String | Microsoft ]]|
| `head` / `tail` | it's complicated | | see [[ documentation/windows/powershell#head-and-tail|below]]
| `ls` | `ls` / `dir` / `Get-ChildItem` | [[https://ss64.com/ps/get-childitem.html| ss64]], [[ https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem | Microsoft ]] | Acts like `ls -l`, no easy equiv of just `ls`
| `pushd` | `pushd` / `Push-Location` | [[ https://ss64.com/ps/push-location.html | ss64 ]] | Works in DOS too, less annoying than `cd` there in fact since it'll traverse filesystem boundaries |
| `tail -f` | `Get-Content -Wait` | | Easily chokes on bad characters and if it does so it actually won't wait, so in practice often just doesn't work |
| `top` | see [[ documentation/windows/powershell#top|below]] | see [[ documentation/windows/powershell#top|below]] | see [[ documentation/windows/powershell#top|below]] |
| `tree` | `tree /F` | [[ https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/tree | Microsoft ]] | `tree` looks fine on screen, but attempts to grep or FindStr fill the output with ????s. By default only prints folders, print files too with `/F`. **This is a basic EXE, it is NOT an alias for a PowerShell equivalent.**|
| `unzip` | `Expand-Archive` | [[https://ss64.com/ps/expand archive.html | ss64]], [[ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/expand-archive | Microsoft ]] |
| `wget`| `Invoke-WebRequest` | [[https://ss64.com/ps/invoke-webrequest.html | ss64]], [[https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest | Microsoft]]| In fact running `wget` will already get you `Invoke-WebRequest`, which [[ https://superuser.com/a/1410881 | saves the content to an object in memory ]] rather than defaulting to file output like `wget` does, making this a surprising alias. You'll need to run, say, `wget url://etc -OutFile filename.exe`. |
== Alternatives and complexities ==
=== head and tail ===
You can use `Get-Content -Last 10` / `Get-Content -First 10` (documentation: [[ https://ss64.com/ps/get-content.html | ss64 ]], [[ https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Get-Content | Microsoft ]]) but only for text files.
Things in PowerShell tend to be objects, which is cool I guess but makes everything more awkward too. So for example for `ls -ltr | tail` you'll have to do something like `Get-ChildItem '..\path\' | Sort-Object -Property LastWriteTime | Select-Object -Last 10`.
=== top ===
You almost just have to grab some equivalent compiled for Windows (for example [[ https://github.com/ClementTsang/bottom/ | bottom aka btm]] is a nice cross-platform atop-like program). Install with `winget install Clement.bottom`.
The best you can really do natively in PowerShell is [[ https://superuser.com/questions/176624/linux-top-command-for-windows-powershell | crazy one-liners]] like for example:
```While(1) { $p = get-counter '\Process(*)\% Processor Time'; cls; $p.CounterSamples | sort -des CookedValue | select -f 15 | ft -a}```
=== less and cat ===
For a nicer equivalent of the `less` and `cat` commands, [[ https://github.com/sharkdp/bat | bat aka batcat ]] is the way to go. `winget install sharkdp.bat`.
== Environment variables ==
Access normal environment variables with `$env:VARIABLENAME`, ie. instead of `%USERPROFILE%` for Windows' equivalent of `$HOME` it's `$env:USERPROFILE`. Contain in curly braces to avoid ambiguity in parsing, ex. `${env:USERPROFILE}`.
== Examples ==
```
lang=powershell
# Get the current state of scheduled tasks with the string "T401" in their name
schtasks | Select-String -Pattern "T401"
# See if anything with "notepad" in the executable name are running
tasklist | Select-String -Pattern "notepad"
# Get all the version info on all the EXEs in the local directory
Get-ChildItem "*.exe" | % {$_.VersionInfo} | Select *
# Get only some info
Get-ChildItem "*.exe" | % {$_.VersionInfo} | Select OriginalFilename,FileVersionRaw
```
== External links for other problems ==
* https://stackoverflow.com/questions/53834304/how-do-i-git-clone-from-a-windows-machine-over-ssh
* [[ https://github.com/PowerShell/Win32-OpenSSH/issues/1082 | Remote SSH commands require double escaping before hitting the DefaultShell ]]
PowerShell is a hilariously verbose command environment, both in terms of every damn command and also in terms of what it spits out at you when something is wrong---at least, at the times where it doesn't just refuse to say anything at all. Unhelpfully verbose or unhelpfully taciturn seem to be its only two modes! However, PowerShell is necessary in many scenarios thanks to being more featureful and modern than Windows' vestigial `cmd.exe` environment.
So here are some important things, presuming you already know how to use normal shells on #Linux or such. And if you don't, stop reading this and learn to be comfortable there first; it's a much less user-hostile place to be, far fewer lines of red error text suddenly thrust upon you. Seriously, it's generally far less of a hassle even on Windows to just use the [[ documentation/windows/wsl/ ]]! (You can even pipe to it, ex `dir | wsl grep -i SomeFilenameIAmLookingFor`.)
== Running normal commands with quoted command or argument paths ==
When using quoted arguments or even just commands, PowerShell goes "oh hey strings" because fucking everything's an object in PowerShell and so suddenly it's thinking you're trying to do string operations when you're just trying to run a damn command with a space in it or something.
The trick is to prepend `& ` to the start of the command. Ex.
```& "C:\path\to\someplace some asshole put space\something.exe" "D:\Silly Path\"```
== Basic Equivalents ==
| Posix-y command | PowerShell equivalent | Documentation | Notes
| --------------------- | -------------------------- | ------- | ------ |
| `ls` | `ls` / `dir` /| `cat` | `Get-ChildItemontent` | [[ https://ss64.com/ps/get-childitemontent.html | ss64 ]], [[ https://docslearn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitemontent | Microsoft ]] | Acts like `ls -l`, no easy equiv of just `ls`| |
| `grep` | `Select-String -Pattern` | [[ https://ss64.com/ps/select-string.html | ss64 ]], [[ https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Select-String | Microsoft ]]|
| `pushd` | `pushd` / `Push-Location` | [[ https://ss64.com/ps/push-location.html | ss64 ]] | Works in DOS too, less annoying than `cd` there in fact since it'll traverse filesystem boundaries |head` / `tail` | it's complicated | | see [[ documentation/windows/powershell#head-and-tail|below]]
| `cat` || `ls` | `ls` / `dir` / `Get-ContenthildItem` | [[ https://ss64.com/ps/get-contenthilditem.html | ss64 ]], [[ https://learndocs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content | Microsoft ]]|hilditem | Microsoft ]] | Acts like `ls -l`, no easy equiv of just `ls`
| `pushd` | `pushd` / `Push-Location` | [[ https://ss64.com/ps/push-location.html | ss64 ]] | Works in DOS too, less annoying than `cd` there in fact since it'll traverse filesystem boundaries |
| `tail -f` | `Get-Content -Wait` | | Easily chokes on bad characters and if it does so it actually won't wait, so in practice often just doesn't work |
| `top` | see [[ documentation/windows/powershell#top|below]] | see [[ documentation/windows/powershell#top|below]] | see [[ documentation/windows/powershell#top|below]] |
| `tree` | `tree /F` | [[ https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/tree | Microsoft ]] | `tree` looks fine on screen, but attempts to grep or FindStr fill the output with ????s. By default only prints folders, print files too with `/F`. **This is a basic EXE, it is NOT an alias for a PowerShell equivalent.**|
| `unzip` | `Expand-Archive` | [[https://ss64.com/ps/expand archive.html | ss64]], [[ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/expand-archive | Microsoft ]] |
| `wget`| `Invoke-WebRequest` | [[https://ss64.com/ps/invoke-webrequest.html | ss64]], [[https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest | Microsoft]]| In fact running `wget` will already get you `Invoke-WebRequest`, which [[ https://superuser.com/a/1410881 | saves the content to an object in memory ]] rather than defaulting to file output like `wget` does, making this a surprising alias. You'll need to run, say, `wget url://etc -OutFile filename.exe`. |
== Alternatives and complexities ==
=== head and tail ===
You can use `Get-Content -Last 10` / `Get-Content -First 10` (documentation: [[ https://ss64.com/ps/get-content.html | ss64 ]], [[ https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Get-Content | Microsoft ]]) but only for text files.
Things in PowerShell tend to be objects, which is cool I guess but makes everything more awkward too. So for example for `ls -ltr | tail` you'll have to do something like `Get-ChildItem '..\path\' | Sort-Object -Property LastWriteTime | Select-Object -Last 10`.
=== top ===
You almost just have to grab some equivalent compiled for Windows (for example [[ https://github.com/ClementTsang/bottom/ | bottom aka btm]] is a nice cross-platform atop-like program). Install with `winget install Clement.bottom`.
The best you can really do natively in PowerShell is [[ https://superuser.com/questions/176624/linux-top-command-for-windows-powershell | crazy one-liners]] like for example:
```While(1) { $p = get-counter '\Process(*)\% Processor Time'; cls; $p.CounterSamples | sort -des CookedValue | select -f 15 | ft -a}```
=== less and cat ===
For a nicer equivalent of the `less` and `cat` commands, [[ https://github.com/sharkdp/bat | bat aka batcat ]] is the way to go. `winget install sharkdp.bat`.
== Environment variables ==
Access normal environment variables with `$env:VARIABLENAME`, ie. instead of `%USERPROFILE%` for Windows' equivalent of `$HOME` it's `$env:USERPROFILE`. Contain in curly braces to avoid ambiguity in parsing, ex. `${env:USERPROFILE}`.
== Examples ==
```
lang=powershell
# Get the current state of scheduled tasks with the string "T401" in their name
schtasks | Select-String -Pattern "T401"
# See if anything with "notepad" in the executable name are running
tasklist | Select-String -Pattern "notepad"
# Get all the version info on all the EXEs in the local directory
Get-ChildItem "*.exe" | % {$_.VersionInfo} | Select *
# Get only some info
Get-ChildItem "*.exe" | % {$_.VersionInfo} | Select OriginalFilename,FileVersionRaw
```
== External links for other problems ==
* https://stackoverflow.com/questions/53834304/how-do-i-git-clone-from-a-windows-machine-over-ssh
* [[ https://github.com/PowerShell/Win32-OpenSSH/issues/1082 | Remote SSH commands require double escaping before hitting the DefaultShell ]]