yo_bee
asked on
Trouble getting Powershell IF THEN to work
I have a question about IF THEN in powershell.
I have a text file that contains First names
I have an array with $Folder.fullname or $Folder.name
I want to ignore all items that match Firstname
I have a text file that contains First names
I have an array with $Folder.fullname or $Folder.name
I want to ignore all items that match Firstname
$firstname = Get-Content -Path 'C:\temp\file.txt'
$Folders = Get-Childitem -path '\\Server1\Folders' -Direcotry
Foreach ($folder in $folders)
{
If ($folder.name -match $firstname)
{
<Do Something>
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
IF worked
Can't reproduce.
Test setup:
Test setup:
PS C:\> Get-Childitem -path 'C:\Folders' -Directory | Select Name
Name
----
Sub0
Sub1
Sub2
Sub3
Sub4
Sub5
PS C:\> Get-Content -Path 'C:\Folders\file.txt'
Sub1
Sub2
Sub3
Sub4
Script 1:PS C:\> Get-Content -Path 'C:\Folders\file.txt'
Sub1
Sub2
Sub3
Sub4
PS C:\> $firstnames = Get-Content -Path 'C:\Folders\file.txt'
PS C:\> Get-Childitem -path 'C:\Folders' -Directory |
>> Where-Object {$firstnames -contains $_.Name} |
>> ForEach-Object {
>> "$($_.Name): Do something"
>> }
>>
Sub1: Do something
Sub2: Do something
Sub3: Do something
Sub4: Do something
Script 2:PS C:\> $firstnames = Get-Content -Path 'C:\Folders\file.txt'
PS C:\> Get-Childitem -path 'C:\Folders' -Directory | ForEach-Object {
>> If ($firstnames -contains $_.Name) {
>> "$($_.Name): Do something"
>> } Else {
>> "$($_.Name): Do something else"
>> }
>> }
>>
Sub0: Do something else
Sub1: Do something
Sub2: Do something
Sub3: Do something
Sub4: Do something
Sub5: Do something else
ASKER
Note:
The text file content looks like this:
Sub1
Sub2
Sub3
Sub4