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
| Tarefa | cmd | Bash | PowerShell |
| listar | dir | ls -la | Get-ChildItem -Force |
| listar tudo abaixo | dir /s | find . | Get-ChildItem -Recurse |
| pasta atual | cd | pwd | Get-Location |
| mudar de pasta | cd pasta | cd pasta | Set-Location pasta |
| criar pasta | md x | mkdir x | New-Item -ItemType Directory x |
| criar arquivo vazio | type nul > a.txt | touch a.txt | New-Item a.txt |
| copiar | copy a b | cp a b | Copy-Item a b |
| copiar pasta | xcopy /e a b | cp -r a b | Copy-Item a b -Recurse |
| mover | move a b | mv a b | Move-Item a b |
| renomear | ren a b | mv a b | Rename-Item a b |
| apagar | del a | rm a | Remove-Item a |
| apagar pasta | rd /s /q x | rm -rf x | Remove-Item x -Recurse -Force |
| existe? | if exist a | [ -e a ] | Test-Path a |
Conteúdo e texto
| Tarefa | cmd | Bash | PowerShell |
| mostrar arquivo | type a | cat a | Get-Content a |
| primeiras linhas | — | head -5 a | Get-Content a -TotalCount 5 |
| últimas linhas | — | tail -5 a | Get-Content a -Tail 5 |
| acompanhar | — | tail -f a | Get-Content a -Wait |
| procurar texto | findstr x a | grep x a | Select-String x a |
| contar linhas | find /c /v "" a | wc -l a | (Get-Content a).Count |
| ordenar | sort a | sort a | Get-Content a | Sort-Object |
| sem repetidos | — | sort -u a | Get-Content a | Sort-Object -Unique |
| trocar texto | — | sed 's/x/y/g' | -replace 'x', 'y' |
| gravar saída | cmd > a | cmd > a | cmd > a ou | Out-File a |
Variáveis e ambiente
| Tarefa | cmd | Bash | PowerShell |
| criar variável | set x=1 | x=1 | $x = 1 |
| usar variável | %x% | $x | $x |
| variável de ambiente | %PATH% | $PATH | $env:PATH |
| definir no ambiente | set X=1 | export X=1 | $env:X = "1" |
| pasta do usuário | %USERPROFILE% | ~ ou $HOME | $HOME ou ~ |
| código de saída | %ERRORLEVEL% | $? | $LASTEXITCODE / $? |
Sistema e rede
| Tarefa | cmd | Bash | PowerShell |
| processos | tasklist | ps aux | Get-Process |
| encerrar | taskkill /im x.exe | pkill x | Stop-Process -Name x |
| onde está o comando | where x | which x | Get-Command x |
| ajuda | x /? | man x | Get-Help x |
| ping | ping host | ping host | Test-Connection host |
| baixar | curl -O url | curl -O url | Invoke-WebRequest url -OutFile a |
| limpar tela | cls | clear | Clear-Host |
| histórico | doskey /history | history | Get-History |
Lógica
| Tarefa | cmd | Bash | PowerShell |
| se | if %x%==1 (...) | if [ $x -eq 1 ]; then ...; fi | if ($x -eq 1) { ... } |
| para cada | for %%f in (*.txt) do ... | for f in *.txt; do ...; done | foreach ($f in Get-ChildItem *.txt) { ... } |
| repetir N vezes | for /l %%i in (1,1,5) do ... | for i in {1..5}; do ...; done | foreach ($i in 1..5) { ... } |
| função | :rotulo + call :rotulo | f() { ...; } | function F { ... } |
| e / ou | && / || | && / || | && / || (PowerShell 7) |
| comentário | rem 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 ...) { }.