PowerShell Cheat Sheet – Roland Edition

Focused on logs, XML (SII), CSV, automation, and system control.

1. Basics

1.1 Help & discovery

# Help
Get-Help Select-String -Online

# Search for commands
Get-Command *csv*

1.2 Useful aliases

ls   # Get-ChildItem
cd   # Set-Location
cat  # Get-Content
rm   # Remove-Item
cp   # Copy-Item
mv   # Move-Item

2. File operations

2.1 Navigation

Set-Location "C:\Users\rfors\OneDrive\job\MM\JSM contabilidad"
Get-ChildItem
Get-ChildItem -Recurse
Get-ChildItem -Recurse -Filter *.xml

2.2 Copy / Move / Delete

Copy-Item file.xml .\backup\
Move-Item *.log .\logs\
Remove-Item *.tmp -WhatIf

3. Searching text in many files

3.1 Recursive invoice search

Get-ChildItem -Recurse -File |
  Select-String -Pattern "34-AX25-001121" -SimpleMatch

3.2 Only show filenames

Get-ChildItem -Recurse -File |
  Select-String "34-AX25-001121" -SimpleMatch |
  Select-Object -Unique Path

3.3 Filter by extension

Get-ChildItem -Recurse -Filter *.xml |
  Select-String "34-AX25-001121" -SimpleMatch

3.4 Tail log file

Get-Content .\sii_validator.log -Tail 100 -Wait |
  Select-String "ERROR"

4. XML Handling

4.1 Load XML

$xml = [xml](Get-Content "file.xml")

4.2 Extract invoice numbers & totals

$xml.Envelope.Body.SuministroLRFacturasEmitidas.RegistroLRFacturasEmitidas |
  ForEach-Object {
      [PSCustomObject]@{
          NumSerie = $_.IDFactura.NumSerieFacturaEmisor
          Total    = $_.FacturaExpedida.ImporteTotal
      }
  }

4.3 Find invoices missing "ClaveRegimen"

$xml.Envelope.Body.SuministroLRFacturasEmitidas.RegistroLRFacturasEmitidas |
  Where-Object { -not $_.FacturaExpedida.ClaveRegimenEspecialOTrascendencia } |
  Select-Object @{
      N='Factura';E={$_.IDFactura.NumSerieFacturaEmisor}
  }, @{
      N='Importe';E={$_.FacturaExpedida.ImporteTotal}
  }

5. CSV Operations

5.1 Load CSV

$csv = Import-Csv "ventas.csv"

5.2 Filter: invoices bigger than 1000€

$csv | Where-Object { [decimal]$_.Total -gt 1000 }

5.3 Group by customer & sum

$csv |
  Group-Object Customer |
  ForEach-Object {
      [PSCustomObject]@{
          Customer = $_.Name
          Suma     = ($_.Group | Measure-Object -Property Total -Sum).Sum
      }
  } |
  Sort-Object Suma -Descending |
  Export-Csv "resumen-clientes.csv" -NoTypeInformation

6. Processes, Services, External Tools

6.1 Monitor heavy processes

Get-Process | Sort-Object CPU -Descending | Select-Object -First 10

6.2 Restart a service

Restart-Service -Name "MyServiceName"

6.3 Python, kubectl, git

python script.py
kubectl get pods
git status

7. Ten Key Patterns for Automation

Pattern 1 – Get → Where → Select

Get-ChildItem -Recurse -Filter *.xml |
  Where-Object { $_.Length -gt 1MB } |
  Select-Object FullName, Length

Pattern 2 – Text search engine

Get-ChildItem -Recurse -File |
  Select-String "34-AX25-001121" -SimpleMatch

Pattern 3 – ForEach to create structured output

$xml... |
  ForEach-Object {
      [PSCustomObject]@{
          Factura = $_.IDFactura.NumSerieFacturaEmisor
          Total   = $_.FacturaExpedida.ImporteTotal
      }
  }

Pattern 4 – Group and sum

Import-Csv "ventas.csv" |
  Group-Object Customer |
  ForEach-Object {
      [PSCustomObject]@{
          Customer = $_.Name
          Total    = ($_.Group | Measure-Object Amount -Sum).Sum
      }
  }

Pattern 5 – Try/Catch

try {
    $xml = [xml](Get-Content "file.xml" -ErrorAction Stop)
} catch {
    Write-Warning $_.Exception.Message
}

Pattern 6 – Lookup tables (hash maps)

$codes = @{ '10'='Telecom'; '01'='General' }
$codes['10']

Pattern 7 – Splatting

$p = @{
    Path="C:\SII"
    Filter="*.xml"
    Recurse=$true
}
Get-ChildItem @p

Pattern 8 – Custom function

function Find-Invoice {
    param($Root, $Invoice)
    Get-ChildItem $Root -Recurse -File |
      Select-String $Invoice -SimpleMatch
}

Pattern 9 – Export to CSV/JSON

... | Export-Csv "out.csv" -NoTypeInformation
... | ConvertTo-Json | Out-File "out.json"

Pattern 10 – Scheduled script pattern

# nightly log check
Get-ChildItem "C:\SII" -Recurse -Filter *.log |
  Select-String "error" |
  Out-File "C:\SII\_reports\errors.log"