3

I have a problem:

I need the user to input the Path (of files) that will be used later but I get this error:

My problem is that the folder has files in it but I just need the Path of the entered folder:

Input should be:

C:\Users\USER1\Desktop\test\files

And I'd need that Path to be put in the variable $Source_UIP

My Powershell error is this:

Set-Location : Cannot convert 'System.Object[]' to the type 'System.String' required by             parameter >'Path'. Specified method is not supported.
At Z:\PS_Hash_Compare.ps1:14 char:4
+ cd $Source_UIP
+    ~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Location], ParameterBindingException
    + FullyQualifiedErrorId :     CannotConvertArgument,Microsoft.PowerShell.Commands.SetLocationCommand

This is my code:

$Source_UIP = Get-ChildItem (Read-Host -Prompt 'Get path of the source files')
cd $Source_UIP
$Hash_Src = Get-Filehash -Algorithm SHA256 (Get-ChildItem -Recurse | Get-Item | where {!$_.PsIsContainer}

and this would be the final use:

$Compare = Compare-Object -ReferenceObject $Hash_Src -DifferenceObject $Hash_Extr -Property hash -IncludeEqual -PassThru | Format-List

any Ideas?

EDIT: many thanks to @Santiago Squarzon for the help! This is my working script. It works also without the Validation step. This is usefull if I enter a "wrong" Path with e.g. a *.txt (= File) instead of a path

$Date_Time = Get-Date -Format "yyyyMMdd_HH_mm"

$ErrorActionPreference = 'Stop'

[ValidateScript({
if(-not (Test-Path -Path $_ -PathType Container))
{
    throw 'Invalid Path'
}

$true
})]
$Source_UIP = Read-Host -Prompt 'Get path of the source files'

[ValidateScript({
if(-not (Test-Path -Path $_ -PathType Container))
{
    throw 'Invalid Path'
}

$true
})]
$Extracetd_UIP = Read-Host -Prompt 'Get path of the Destination files'



#Compare

$Hash_Src = Get-ChildItem $Source_UIP -Recurse -File | Get-FileHash -Algorithm SHA256

$Hash_Extr = Get-Filehash -Algorithm SHA256 (Get-ChildItem -Recurse | Get-Item | where {!$_.PsIsContainer})
$Hash_Extr = Get-ChildItem $Extracetd_UIP -Recurse -File | Get-FileHash -Algorithm SHA256


$Compare = Compare-Object -ReferenceObject $Hash_Src -DifferenceObject $Hash_Extr -Property hash -IncludeEqual -PassThru | Format-List

$Compare | Out-File -FilePath "C:\Users\USER1\Desktop\test\Destination\Hash_Result-$Date_Time.txt"

Invoke-Item -Path "C:\Users\USER1\Desktop\test\Destination\Hash_Result-$Date_Time.txt"
2
  • The error is likely to be because Get-ChildItem found more than 1 item (file / directory) on your Read-Host statement. You should clarify what you're trying to accomplish on your code as there is likely to be a better way of doing this. Commented Nov 22, 2021 at 17:06
  • 1
    @SantiagoSquarzon Yes this is the case. There are multiple files. I will update the question, thanks :) Commented Nov 22, 2021 at 17:07

1 Answer 1

3

The right approach, in my opinion, would be first to check if the user's input is valid, meaning that the path exists and the path is a folder.

For that, you can use many approaches, one I like to use is the ValidateScript validation attribute.
One thing to note, see that I'm not using Set-Location but passing the path (user input) directly to Get-ChildItem once we confirmed that the path is a folder, it's up to you if you wish to do it this way or with cd.

$ErrorActionPreference = 'Stop'

[ValidateScript({
    if(-not (Test-Path -Path $_ -PathType Container))
    {
        throw 'Invalid Path'
    }

    $true
})]
$Source_UIP = Read-Host -Prompt 'Get path of the source files'

# where {!$_.PsIsContainer} => Can be replaced with -File
$Hash_Src = Get-ChildItem $Source_UIP -Recurse -File | Get-FileHash -Algorithm SHA256
Sign up to request clarification or add additional context in comments.

10 Comments

that worked! This is a good working solution, thanks a lot! :)
@Timmmsa happy to help!
but I don't quite get why my Method didn't work... is it because I sent the Path (Object?) directly to the variable?
@Timmmsa $Source_UIP = (Get-Item (Read-Host -Prompt 'Get path of the source files')).FullName works for me as long as I give a valid path but again, this doesn't validate if the path is a file or a directory. By the way, $ErrorActionPreference is an automatic variable and you should leave it as is (at the top of your script). $ErrorActionPreference1 or $ErrorActionPreference2 doesn't mean anything to PowerShell.
@Timmmsa correct, this will define how PowerShell will act when it finds non-terminating errors.
|

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.