I am using the following code to select a folder through the Windows Forms “Browse” function and then pass that path to the gci cmdlet
cls
Function Get-Directory($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
$OpenfolderDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$OpenfolderDialog.RootFolder = $initialDirectory
$OpenfolderDialog.ShowDialog()| Out-Null
$StartDir = $OpenfolderDialog.SelectedPath
Return $StartDir | Out-String
}
$myDir = Get-Directory -initialDirectory "Desktop"
$Child = gci -path $mydir -r -Filter *.jpg
Foreach ($item in $Child) {Move-Item -path $item.pspath -Destination $myDir -Force}
but I get these errors:
***At C:\Test\Combine Pics2.ps1:17 char:13
+ $Child = gci <<<< -path $mydir -r -Filter *.jpg
+ CategoryInfo : ObjectNotFound: (C:\Test
:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Move-Item : Cannot bind argument to parameter ‘Path’ because it is null.
At C:\Test\Combine Pics2.ps1:19 char:43
+ Foreach ($item in $Child) {Move-Item -path <<<< $item.pspath -Destination $myDir -Force}
+ CategoryInfo : InvalidData: (:) [Move-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.MoveItemCommand***
The $myDir variable is of type String, why does it not pass to the -path parameter.
I got a newline and carriage return at the end of the
$mydirvalue, so try trimming with something like this to see if that is your issue:Update: Better yet, just lose the
Out-Stringin your function: