How can i get a prompt to ask for the path, when using Get-ChildItem in PowerShell?

14,490

Solution 1

Use Read-Host:

Get-ChildItem -Path (Read-Host -Prompt 'Get path')

Solution 2

Here's an example, FWIW:

 $source,$target = $null,$null

while ($source -eq $null){
$source = read-host "Enter source file name"
if (-not(test-path $source)){
    Write-host "Invalid file path, re-enter."
    $source = $null
    }
elseif ((get-item $source).psiscontainer){
    Write-host "Source must be a file, re-enter."
    $source = $null
    }
}

while ($target -eq $null){
$target = read-host "Enter source directory name"
if (-not(test-path $target)){
    Write-host "Invalid directory path, re-enter."
    $target = $null
    }
elseif (-not (get-item $target).psiscontainer){
    Write-host "Target must be a directory, re-enter."
    $target = $null
    }
}
Share:
14,490
Mark Jenster
Author by

Mark Jenster

Updated on August 01, 2022

Comments

  • Mark Jenster
    Mark Jenster over 1 year

    I'm rather new to PowerShell. I found a few guides and mashed a little script together, though i do not seem to be able to set a prompt to ask for the source/destination.

    The script is as following:

    gci -path   | Get-Random -Count 4 | mi -Destination C:\Temp
    while(1) { sleep -sec 20; .\Power.ps1 }
    

    For any response, thanks in advance!