Difference between revisions of "PowerShell"
From Torben's Wiki
Jump to navigationJump to searchLine 32: | Line 32: | ||
$limit = (Get-Date).AddDays(-90) | $limit = (Get-Date).AddDays(-90) | ||
$scope = Get-ChildItem -Path $dir -Force | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $limit } | $scope = Get-ChildItem -Path $dir -Force | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $limit } | ||
+ | # | ||
# Filters: | # Filters: | ||
# -Filter "*.dfq" | # -Filter "*.dfq" | ||
Line 37: | Line 38: | ||
# $_.PSIsContainer => dirs | # $_.PSIsContainer => dirs | ||
# !$_.PSIsContainer => files | # !$_.PSIsContainer => files | ||
− | + | # | |
− | write scope to CSV file | + | # write scope to CSV file |
$scope | Select-Object FullName, CreationTime | Export-Csv -Path .\$logfile -NoTypeInformation | $scope | Select-Object FullName, CreationTime | Export-Csv -Path .\$logfile -NoTypeInformation | ||
# -Append | # -Append | ||
− | delete files in scope | + | # delete files in scope |
$scope | Remove-Item -Force | $scope | Remove-Item -Force | ||
Latest revision as of 15:49, 12 December 2019
run
single command
powershell -c "$lastmonth = (Get-Date).addMonths(-3); $lastmonth.tostring(\"yyyy-MM\")"
script file
powershell -executionpolicy bypass -File ./del-old-dirs.ps1
echo/print
Write-Host "Hello World" pause
date
get current date e.g. YYYY-MM
powershell -c "$lastmonth = (Get-Date).addMonths(-3); $lastmonth.tostring(\"yyyy-MM\")"
strings
get substring of string
$var = 'Hello World' $result = $var.substring(0, 5) $result-> Hello
merge stings
$dest = $rootPath + "\" +$subFolder
filesystem access
check a dir exists
if (Test-Path $dest) { ... }
del / delete file
Remove-Item -Path "C:\dir\file.txt"
mkdir / create dirctory
New-Item -ItemType directory -Path C:\newDir
select old files from directory
$limit = (Get-Date).AddDays(-90) $scope = Get-ChildItem -Path $dir -Force | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $limit } # # Filters: # -Filter "*.dfq" # Where # $_.PSIsContainer => dirs # !$_.PSIsContainer => files # # write scope to CSV file $scope | Select-Object FullName, CreationTime | Export-Csv -Path .\$logfile -NoTypeInformation # -Append # delete files in scope $scope | Remove-Item -Force
delete old files
delete old files (from [1])
$limit = (Get-Date).AddDays(-15) $path = "C:\Some\Path" # delete files older than the $limit. Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force # delete any empty directories left behind after deleting the old files. Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse
if / else
if folder exists
if (Test-Path $dirDone) { ... }
exit with error code
exit (1)
loops
$dirs = Get-ChildItem -Path .\ -Directory foreach ($d in $dirs) { ... }
net use q: \\server\dir1\dir2 net use q: /delete