cmd e Bash × PowerShell

Uma tabela só, com os três lado a lado. Para entender as diferenças com calma, veja Do cmd para o PowerShell e Do Bash para o PowerShell.

Você vai aprender
  • O equivalente de cada comando nos três ambientes.
  • Variáveis, laços e condicionais lado a lado.

Arquivos e pastas

TarefacmdBashPowerShell
listardirls -laGet-ChildItem -Force
listar tudo abaixodir /sfind .Get-ChildItem -Recurse
pasta atualcdpwdGet-Location
mudar de pastacd pastacd pastaSet-Location pasta
criar pastamd xmkdir xNew-Item -ItemType Directory x
criar arquivo vaziotype nul > a.txttouch a.txtNew-Item a.txt
copiarcopy a bcp a bCopy-Item a b
copiar pastaxcopy /e a bcp -r a bCopy-Item a b -Recurse
movermove a bmv a bMove-Item a b
renomearren a bmv a bRename-Item a b
apagardel arm aRemove-Item a
apagar pastard /s /q xrm -rf xRemove-Item x -Recurse -Force
existe?if exist a[ -e a ]Test-Path a

Conteúdo e texto

TarefacmdBashPowerShell
mostrar arquivotype acat aGet-Content a
primeiras linhas—head -5 aGet-Content a -TotalCount 5
últimas linhas—tail -5 aGet-Content a -Tail 5
acompanhar—tail -f aGet-Content a -Wait
procurar textofindstr x agrep x aSelect-String x a
contar linhasfind /c /v "" awc -l a(Get-Content a).Count
ordenarsort asort aGet-Content a | Sort-Object
sem repetidos—sort -u aGet-Content a | Sort-Object -Unique
trocar texto—sed 's/x/y/g'-replace 'x', 'y'
gravar saídacmd > acmd > acmd > a ou | Out-File a

Variáveis e ambiente

TarefacmdBashPowerShell
criar variávelset x=1x=1$x = 1
usar variável%x%$x$x
variável de ambiente%PATH%$PATH$env:PATH
definir no ambienteset X=1export X=1$env:X = "1"
pasta do usuário%USERPROFILE%~ ou $HOME$HOME ou ~
código de saída%ERRORLEVEL%$?$LASTEXITCODE / $?

Sistema e rede

TarefacmdBashPowerShell
processostasklistps auxGet-Process
encerrartaskkill /im x.exepkill xStop-Process -Name x
onde está o comandowhere xwhich xGet-Command x
ajudax /?man xGet-Help x
pingping hostping hostTest-Connection host
baixarcurl -O urlcurl -O urlInvoke-WebRequest url -OutFile a
limpar telaclsclearClear-Host
históricodoskey /historyhistoryGet-History

Lógica

TarefacmdBashPowerShell
seif %x%==1 (...)if [ $x -eq 1 ]; then ...; fiif ($x -eq 1) { ... }
para cadafor %%f in (*.txt) do ...for f in *.txt; do ...; doneforeach ($f in Get-ChildItem *.txt) { ... }
repetir N vezesfor /l %%i in (1,1,5) do ...for i in {1..5}; do ...; doneforeach ($i in 1..5) { ... }
função:rotulo + call :rotulof() { ...; }function F { ... }
e / ou&& / ||&& / ||&& / || (PowerShell 7)
comentáriorem ou ::## ou <# ... #>

Resumo

  • Os apelidos (dir, cd, cat, cp) ajudam na transição, mas os parâmetros são do PowerShell.
  • Variáveis de ambiente: $env:NOME.
  • Laços e condicionais usam parênteses e chaves: if (...) { }, foreach (... in ...) { }.