-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileCommands.ps1
More file actions
33 lines (28 loc) · 1.74 KB
/
FileCommands.ps1
File metadata and controls
33 lines (28 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Create file with variable name of `x`
New-Item x -Type file
# Bulk change of file extension
# In this case, I'm changing a bunch of audiobook CD files from .m4a to .m4b to use them in the audiobook portion of the iTunes/Music app on macOS
Get-ChildItem . -Recurse -Filter *.m4a | Rename-Item -NewName {[System.IO.Path]::ChangeExtension($_.Name, ".m4b")}
# Equivalent with an explicit setting of the `-Path` parameter (implied above)
Get-ChildItem . -Recurse -Filter *.m4a | % { Rename-Item -Path $_ -NewName ([System.IO.Path]::ChangeExtension($_.Name, ".m4b")) }
# Get name of current folder (without full path)
# Example: C:\whatever -> "whatever"
# (???: There may be some difference between the current location and where the command was run.)
[System.IO.Path]::GetFileName((Get-Location).Path)
[System.IO.Path]::GetFileName($PWD.Path)
# Are two file byte arrays equal (Note: diff output when not the same is not really useful on byte arrays)
-not (Compare-Object -ReferenceObject ([System.IO.File]::ReadAllBytes("path\to\file1")) -DifferenceObject ([System.IO.File]::ReadAllBytes("path\to\file2")))
Get-ChildItem -Recurse -File | Sort-Object -Property Name | % { Get-FileHash -Path $_ -Algorithm MD5 }
Get-ChildItem -Recurse -File | Sort-Object -Property Name | % { Get-FileHash -Path $_ -Algorithm SHA256 }
# Compare file hash to known valid value
$ValidHash = "ABC123"
$CalculatedHash = Get-FileHash -Path '~\Downloads\SomeFile.zip' -Algorithm MD5 # Or SHA256, etc.
$ValidHash -eq $CalculatedHash.Hash
# Get all folders in a directory
Get-ChildItem -Directory
Get-ChildItem -Path {whatever} -Directory
# Get all hidden items in a directory
Get-ChildItem -Hidden
Get-ChildItem -Path {whatever} -Hidden
# Hidden directories
Get-ChildItem -Path {whatever} -Hidden -Directory