Skip to content

Instantly share code, notes, and snippets.

@fvilante
Last active May 27, 2022 12:03
Show Gist options
  • Save fvilante/d6381b4d3c883c9277d93514acb9d26e to your computer and use it in GitHub Desktop.
Save fvilante/d6381b4d3c883c9277d93514acb9d26e to your computer and use it in GitHub Desktop.
powershell tips and tricks
#Get help
Get-Help Sort-Object
#Old dir
dir
Get-ChildItem
Get-ChildItem *.txt
Get-ChildItem -Directory
Get-ChildItem -File
#dir recursive
Get-ChildItem -Recurse
Get-ChildItem -Recurse -Depth 2
#Show content inside a file
Get-Content c:\filename
cat #alias to Get-Content
#For each object, replace content, write to a new file
Get-Content .\.gitignore | ForEach-Object {$_.ToString().Replace('.idea','.idea do flavio')} | Set-Content 'juca_temp.txt'
#Pipe: Show the content of txt files in current directory
Get-ChildItem *.txt | Get-Content
#Where-Object
$exclude = @(".cs", ".tt", ".xaml", ".csproj", ".sln", ".xml", ".cmd", ".txt")
Get-ChildItem -Path $path -Recurse | Where-Object { $exclude -notcontains $_.Extension }
dir -r -Directory | ? {$_.fullname -notlike "*node_modules*"}
#Save in a variable
$a = Get-ChildItem
#ForEach-Object
$b = Get-ChildItem | ForEach-Object {$_.fullname}
#List only some files recursive
$exclude = @(".cs", ".tt", ".xaml", ".csproj", ".sln", ".xml", ".cmd", ".txt")
Get-ChildItem -Path $path -Recurse | Where-Object { $exclude -notcontains $_.Extension }
#Select-String
$c = $b | Select-String 'tsconfig'
#
#aliases for DOS and Linux command-line
Get-Alias
#Select a string
# Using Get-childItem to get a list of files modified in last 3 days
(Get-ChildItem -Path c:\pstbak\*.* -Filter *.pst | ? {
$_.LastWriteTime -gt (Get-Date).Date.AddDays(-3)
}).Count
# List files by extensions
@fvilante
Copy link
Author

fvilante commented May 2, 2022

Procurar por clientes que usam 2 eixos micro

##### Dentro da pasta Z-clientes, listar todos os clientes que utilizam o 2 eixos micro
# cria job
cd "T:\1-Clientes"
$job = dir "2 Eixos Micro.xlsx" -recurse -Depth 10 &
# le job assincrono
$a = Receive-Job -Job $job -Keep
# lista clientes
$b = $a | ForEach-Object { ($_.fullname).split('\')[2] } |  Sort-Object -Unique

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment