0
votes

qqq.exe exists.

$DB is correct, I use it other times just fine.

$DB = Get-Content C:\Users\asd\Desktop\Farm\AccountList.txt
foreach ($x in $DB){
    Rename-Item C:\Users\asd\Desktop\Farm\$x\qqq.exe $x.exe
}

Rename-Item : Cannot bind argument to parameter 'NewName' because it is null. At C:\Users\asd\Desktop\Farm\test2.ps1:3 char:57 + Rename-Item C:\Users\asd\Desktop\Farm\$x\qqq.exe $x.exe + ~~~~~~ + CategoryInfo : InvalidData: (:) [Rename-Item], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RenameItemCommand

1
$x.exe -> "$x.exe"Mathias R. Jessen

1 Answers

1
votes

When the powershell parser sees an argument starting with a $, it treats it as an expression, meaning that it will try to evaluate it as code.

Since the string $x doesn't have a property named exe, the expression results in $null and Rename-Item throws the error you see.

If you use a double-quoted string instead, the parser will stop evaluating $x after the dot:

Rename-Item C:\Users\asd\Desktop\Farm\$x\qqq.exe "$x.exe"

From Get-Help about_Parsing:

When processing a command, the Windows PowerShell parser operates
in expression mode or in argument mode: 

    - In expression mode, character string values must be contained in
      quotation marks. Numbers not enclosed in quotation marks are treated
      as numerical values (rather than as a series of characters). 

    - In argument mode, each value is treated as an expandable string 
      unless it begins with one of the following special characters: dollar
      sign ($), at sign (@), single quotation mark ('), double quotation
      mark ("), or an opening parenthesis (().