I have this bellow command to check is folder contains name "AAA_*" in path "C:\Test" exist and if so - make from it name variable:
$DirPath = "C:\Test"
$DirName = "AAA"
If (Test-Path -Path ($DirPath "\" $DirName "_*")) {
C:;cd\;cd ($DirPath "\" $DirName "_*")
$NameFromDir = pwd | Select-Object | %{$_.ProviderPath.Split("\")[-1]}
cd $PSScriptRoot}
I have to double write it path, and also i need to go to inside of it to set is as variable.
Is this can be done in another shorten or easier way?
CodePudding user response:
Try this ($NameFromDir will be empty if path does not exist):
$DirPath = "C:\Test"
$DirName = "AAA"
$NameFromDir = (Get-Item "$DirPath\$DirName_*").FullName
If you need to restrict the result to a single element you can add
| select-object -First 1