Skip to content

PowerShell

Lowy Shin edited this page Mar 4, 2020 · 9 revisions

Basic information and tips

Advanced tips

  • Export-Csv

    • ex : Get-EventLog system | Export-Csv -path syslog.csv -Encoding UTF8 -NoTypeInformation -Delimiter `t
      • -path : export path
      • -Encoding : UTF8, Default
      • -NoTypeInformation : delete first information line
        • #TYPE System.Diagnostics.EventLogEntry#system/Microsoft-Windows-GroupPolicy/1500
      • -Delimiter : `t, ,
  • CPU usage

$cpuinfo = New-Object -TypeName PSCustomObject
$cpu = Get-WmiObject Win32_PerfFormattedData_PerfOS_Processor | ?{$_.Name -match "^[0-9]+$"}
$cpuCnt = 0
foreach($c in $cpu)
{
    $cpuinfo | Add-Member -MemberType NoteProperty -Name $c.Name -Value $c.PercentProcessorTime
    $cputotal = $cputotal + $c.PercentProcessorTime
    $cpuCnt = $cpuCnt + 1
}
$cputotal = $cputotal / $cpuCnt
$cputotal
# for Debug
# Write-Host "CPU Count : $cpuCnt"
# Write-Host "CPU Detail : "
# $cpuinfo
  • Split files by line
# Read parent CSV
$InputFilename = Get-Content '.\source.csv'
$OutputFilenamePattern = 'output_done_'
$LineLimit = 5000
 
# Initialize
$line = 0
$i = 0
$file = 0
$start = 0
 
# Loop all text lines
while ($line -le $InputFilename.Length) {
 
    # Generate child CSVs
    if ($i -eq $LineLimit -Or $line -eq $InputFilename.Length) {
        $file++
        $Filename = "$OutputFilenamePattern$file.csv"
        $InputFilename[$start..($line - 1)] | Out-File $Filename -Force
        $start = $line;
        $i = 0
        Write-Host "$Filename"
    }
 
    # Increment counters
    $i++;
    $line++
}

File handling

  • Compare contents(text) with FileA and FileB
    • diff (cat FileA) (cat FileB)

Math

  • Round
    • [Math]::Round( 4912.83875 , <position> )
      • * 0 : 4912.83875 * 2 : 4900 * -2 : 4912.84

sample

$num = [Math]::Round(1.4999, -2);

Translate this page?

Data(DBMS, NoSQL)

Development

Tools

Management

OS

Hardware

Business

Hobby

Lifestyle

Giip(RPA Engine)

Clone this wiki locally