0

I added in New-Item command but it's not liking my syntax. Please help:

$Dir = get-childitem -path "\\ahs-bind01\ftptest01\CRAR" -recurse
 $Source = $Dir | where {$_.extension -eq ".txt"}

#Declare the file path and sheet name

$file = "C:\Users\us6205\Desktop\DatabaseNameConfigs\Test\CareMoveFileParentDestPaths.xlsx"

$sheetName = "sheet1" 


 #Create an instance of Excel.Application and Open Excel file

$objExcel = New-Object -ComObject Excel.Application

$workbook = $objExcel.Workbooks.Open($file) 

$sheet = $workbook.Worksheets.Item($sheetName)

$objExcel.Visible=$false 


 #Count max row

$rowMax = ($sheet.UsedRange.Rows).count 

#Declare the starting positions

$rowLOC,$colLOC = 1,1
 $rowUNC,$colUNC = 1,2 


 #loop to get values and store it 

for ($i=1; $i -le $rowMax-1; $i++)

{ 

$LOC = $sheet.Cells.Item($rowLOC+$i,$colLOC).text
 $UNC = $sheet.Cells.Item($rowUNC+$i,$colUNC).text 

$Path = Get-ChildItem $UNC -recurse |
 Where-Object { ($_.PSIsContainer -eq $true) -and  ( $_.Name -match "$Source") } 
 $Dest = $Path | New-Item -path $Dest -itemtype directory | Move-Item -Path $Source -Destination $Dest -Force
 }

#close excel file

$objExcel.quit() 

Excel Doc:

Col A       Col B
Test C:\Users\US6205\Desktop\Test\Unprocessed 
Test1 C:\Users\US6205\Desktop\Test1\Unprocessed 

Error

3
  • The error is pretty clear. The $dest variable doesn't seem to have a value at the point that you try to use it. Try shortening up your pipeline and figuring out what it does hold at that point and why it doesn't hold what you think it should. Commented Apr 26, 2016 at 15:36
  • I will also point out that $path will be zero, one, or more FileSystem objects and the -Path parameter of New-Item expects one or more strings. So at the very least I would expect a type mismatch. Commented Apr 26, 2016 at 15:42
  • #loop to get values and store it for ($i=1; $i -le $rowMax-1; $i++) { $LOC = $sheet.Cells.Item($rowLOC+$i,$colLOC).text $UNC = $sheet.Cells.Item($rowUNC+$i,$colUNC).text $Path = Get-ChildItem $UNC -recurse | Where-Object { ($_.PSIsContainer -eq $true) -and ( $_.Name -match "$Source") } New-Item -Path $UNC -type directory Move-Item -Path $Source -Destination $Path -Force } #close excel file $objExcel.quit() Commented Apr 26, 2016 at 16:17

1 Answer 1

2

The use of pipeline is not correct in below line:

$Dest = $Path | New-Item -path $Dest -itemtype directory | Move-Item -Path $Source -Destination $Dest -Force

This does not assign $Path to $Dest. It passes the $Path variable to next command on the right of the pipeline. $Dest is empty when you are trying to use in New-Item command. Instead, use in separate lines as below:

$Dest = $Path
New-Item -path $Dest -itemtype directory
Move-Item -Path $Source -Destination $Dest -Force

I would also recommend to use Test-Path cmdlet to check that the paths exist in your script.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.