Throws an error: Missing an argument for parameter 'ItemType'. Specify a parameter of type 'System.String' and try again.
由于Deadly-Bagel's helpful answer指出,你错过了一个参数-ItemType
,而是遵循它与另一个参数,-Type
,其实是别名对于-ItemType
- 所以remov ing 要么-ItemType
或-Type
将工作。
要找到一个参数的别名,使用类似(Get-Command New-Item).Parameters['ItemType'].Aliases
Renames my latest folder to _MM-dd-yyyy
, but I want latest_MM-dd-yyyy
.
您可以直接追加日期字符串$localPath
,其中有一个尾随\
,所以$newPath
看起来像'c:\example\latest\_02-08-2017'
,这不是意图。
确保$localPath
有没有尾随\
解决问题,但千万注意,Rename-Item
一般只接受一个文件/目录名为-NewName
的说法,不是一个完整路径;你只能逃脱一个完整路径,如果它的父路径是相同作为输入项目的 - 换句话说,你可以,如果它不会在不同的位置导致重命名的项目只指定路径(你需要的Move-Item
cmdlet来实现这一点)。
如果我们把它们放在一起:
param (
$localPath = "c:\example\latest\" #"# generally, consider NOT using a trailing \
)
# Rename preexisting directory, if present.
if (Test-Path $localPath) {
# Determine the new name: the name of the input dir followed by "_" and a date string.
# Note the use of a single interpolated string ("...") with 2 embedded subexpressions,
# $(...)
$newName="$(Split-Path -Leaf $localPath)_$((Get-Date).AddDays(-1).ToString('MM-dd-yyyy'))"
Rename-Item -Path $localPath -newName $newName
}
# Recreate the directory ($null = ... suppresses the output).
$null = New-Item -ItemType Directory -Force -Path $localPath
需要注意的是,如果你在当天运行此脚本超过一次多,你会在重命名时遇到错误(这很容易处理)。