Move files or directories to the YYYYMM folder using the first 8 characters of the specified file name, YYYYMMDD.
- Microsoft Windows 11
- PowerShell 5.1
# See what happens when you run it.
Move-ItemToArchiveDirectoryByDateInName $HOME\19610412_dark_and_blue.txt -WhatIf
# Move the file to '$HOME\Archive\1961\196104\19610412_dark_and_blue.txt'.
Move-ItemToArchiveDirectoryByDateInName $HOME\19610412_dark_and_blue.txt
Install-Module -Name PSArchiveItemByDate
# Install the module into LocalApp directory.
cd $env:LOCALAPPDATA
gh repo clone kumarstack55/PSArchiveItemByDate
# or, import the module.
Import-Module -Name $env:LOCALAPPDATA\PSArchiveItemByDate\PSArchiveItemByDate\PSArchiveItemByDate.psd1 -Force -Verbose
# If you installed from PowerShell Gellery:
$Version = [String](Get-InstalledModule -Name PSArchiveItemByDate).Version
cd "$env:ProgramFiles\WindowsPowerShell\Modules\PSArchiveItemByDate\$Version"
# or, if you installed from source:
cd "$env:LOCALAPPDATA\PSArchiveItemByDate\PSArchiveItemByDate"
# If you want to run it with "Send to" in Explorer, create a shortcut.
function New-ShortcutLinkItemExecutedByPowershell {
param(
[parameter(Mandatory)][String]$LiteralPath,
[parameter(Mandatory)][String]$Value
)
$ShortcutParentPath = Split-Path $LiteralPath -Parent
$ShortcutParentItem = Get-Item -LiteralPath $ShortcutParentPath
$ShortcutName = Split-Path $LiteralPath -Leaf
$ShortcutFullName = Join-Path $ShortcutParentItem.FullName $ShortcutName
$Ps1Item = Get-Item -LiteralPath $Value
$Ps1FullName = $Ps1Item.FullName
$Shell = New-Object -ComObject WScript.Shell
$Shortcut = $Shell.CreateShortcut($ShortcutFullName)
$Shortcut.TargetPath = "powershell.exe"
$Shortcut.Arguments = "-File `"$Ps1FullName`" -InformationAction Continue"
$Shortcut.Save()
}
$SendToDir = Join-Path $env:APPDATA Microsoft\Windows\SendTo
$ShortcutFilePath = Join-Path $SendToDir "Move Items To Archive Directory.lnk"
New-ShortcutLinkItemExecutedByPowershell -LiteralPath $ShortcutFilePath `
-Value PSArchiveItemByDate.ps1
- Write tests.
- Write documents.