Get-ChildItem results looks like relative paths in Powershell - powershell

I would like to scan and move folders (and sub folders or even deeper) from one folder to another using Powershell.
Currently I'm using this pipe of commands.
Get-ChildItem -recurse -path sub\WORK -filter "* OK" | Where-Object { $_.PSIsContainer } | foreach { Move-Item -path $_ -destination sub\OK }
Unfortunately it doesn't work because the found results are relative to .\sub\WORK, when trying to move them Move-Item complains that the folders are not in the current folder:
Move-Item : Cannot find path 'C:\TMP\2011-12-12 test 2 OK' because it does not exist.
I expect that $_ would contain: 'C:\TMP\sub\WORK\2011-12-12 test 2 OK' because these are objects in Powershell and no strings like in Linux.

In case you use Get-ChildItem, be very careful. The best way is to pipe the objects to Move-Item and you don't need to think about it more:
Get-ChildItem -recurse -path sub\WORK -filter "* OK" | Where-Object { $_.PSIsContainer } | Move-Item -destination sub\OK
(no need to use Foreach-Object)
The main reason why I'm answering is this one: Get-ChildItem constructs object differently, depending on the parameters. Look at examples:
PS C:\prgs\tools\Console2> gci -include * | % { "$_" } | select -fir 5
C:\prgs\tools\Console2\-verbose
C:\prgs\tools\Console2\1UpdateDataRepositoryServices.ps1
C:\prgs\tools\Console2\22-52-59.10o52l
C:\prgs\tools\Console2\2jvcelis.ps1
C:\prgs\tools\Console2\a
PS C:\prgs\tools\Console2> gci | % { "$_" } | select -fir 5
-verbose
1UpdateDataRepositoryServices.ps1
22-52-59.10o52l
2jvcelis.ps1
a
Then if you use $_ in a cycle and PowerShell needs to convert FileInfo from Get-ChildItem to string, it gives different results. That happened when you used $_ as argument for Move-Item. Quite bad.
I think there is a bug that reports this behaviour.

You are correct that objects are being piped down the pipeline instead of strings. This is good in that it is more flexible. The drawback is that if you don't explicitly tell the system which property of the object to use you are at the mercy of the system designers. See if explicitly telling the system the property that you want will help:
Get-ChildItem -recurse -path sub\WORK -filter "* OK" | Where-Object { $_.PSIsContainer } | foreach { Move-Item -path $_.Fullname -destination sub\OK }

I just learn't that the PSPath is automatically used in Copy-Item, Move-Item etc. when you don't specify the source in a pipeline, so something like:
gci .\sub\Work | move-item -Destination .\sub\OK
(simplified example)
would work and it would use the PSPath of the passed object to determine the source.
Since the Get-ChildItem returns objects like you said, you can use Get-Member to see what the object has to offer ( that is know about its properties and methods)
Get-ChileItem path | Get-Member
You could see that FullName is one of the properties that you could use.

Here is what worked for me.
Get-ChildItem -Path .\ -Recurse -filter "* OK" | %{Join-Path -Path $_.Directory -ChildPath $_.Name } | Move-Item -Destination sub\OK

Related

Move all even files to different directory | Powershell and regex

I'm trying to move all even files from the current location to the directory "foo", but I have a problem matching them with regex.
Filenames are in this format:
11.txt, 121.txt, 342.txt
The command I'm currently using is:
Get-ChildItem | Where-Object {$_.Name -match '^[0-9]*[02468]$'} | Move-Item -Destination .\foo
The previous command I was using works OK but only for two-digit files 1.txt-99.txt
Get-ChildItem | Where-Object {$_.Name -match '^[0-9]+[02468]'} | Move-Item -Destination .\foo
I tried at https://regex101.com/ site with .NET flavor and checked this regex ^[0-9]*[02468]$ and it works OK - matches all even numbers, but for some reason, I have a problem with above mention PS command...
An alternate method I have used previously for odds/evens is a division method. The solution Santiago has provided in the comments is also a valid regex method for this problem.
# Gets files and starts loop on files
Get-ChildItem "C:\Temp\AllFiles" -File | ForEach-Object {
# If the BaseName of file is divisible by 2, else
If($_.BaseName % 2 -eq 0) {
Move-Item -Destination "C:\Temp\Evens" -Path $_.FullName
} Else {
Move-Item -Destination "C:\Temp\Odds" -Path $_.FullName
}
}

PowerShell Delete more quickly

I have a PowerShell script that recursively deletes all files and folders, but excludes certain folders as they should not be deleted.
It works 100%, but my problem is performance. I need this to run a lot faster.
Any ideas on how to make this faster?
Write-Host "Purging $InstallationDirectorySite - Deleting files..."
$FolderExlusions = (
"App_Data",
"Logs",
"TEMP",
"ExamineIndexes",
"DistCache",
"GitPathProviderRepository"
)
[regex] $files_regex = "Logs|ExamineIndexes|DistCache*|GitPathProviderRepository*"
if(Test-Path $InstallationDirectorySite) {
Get-ChildItem -Path $InstallationDirectorySite -Recurse -Exclude $FolderExlusions |
Where-Object {$_.FullName -notmatch $files_regex} |
Remove-Item -Recurse
}
else {
Write-Output "$InstallationDirectorySite doesn't exist"
}
You are in fact filtering the excluded folders twice.
The first time using the -Exclude parameter and the second time using a Regex -match.
However, the Exclude parameter takes a string array, not a single string with keywords separated by a comma and a newline as you get from the 'here-string'.
See Get-ChildItem
Also, the regex you use is wrong, because the asteriks * in regex is a quantifier, not a wildcard character.
I suggest you filter once using either the -Exclude parameter like this (here the asteriks is a Wildcard):
$FolderExlusions = "App_Data","Logs","TEMP","ExamineIndexes","DistCache*","GitPathProviderRepository*"
Get-ChildItem -Path $InstallationDirectorySite -Recurse -Exclude $FolderExlusions | Remove-Item -Recurse -WhatIf
Or use only the regex method in a Where-Object clause like this:
$FolderExlusions = "^(App_Data|Logs|TEMP|ExamineIndexes|DistCache.*|GitPathProviderRepository.*)"
Get-ChildItem -Path $InstallationDirectorySite -Recurse | Where-Object { $_.Name -notmatch $FolderExlusions } | Remove-Item -Recurse -WhatIf
Remove the -WhatIf if you are satisfied with the results.
Hope that helps

File Sorting Based on Similar File and Folder Names

Im still generally new to powershell, and I am trying to create a program that will take files based on their name, and move them into folders that have a similar name but not exactly the same.
For example, Lets say I have 3 files, Apples.txt, Grapes.txt, and Oranges.txt. And I want to move them into corresponding folders, ApplesUSA, GrapesNY, OrangesFL.
I could just hard code it using a loop and a If-Then Statement. i.e If Apples.txt exists move to ApplesUSA. But I want it to be dynamic, so if other files and folders are added later I dont have to update the code. Is there a way to write a statement that would say if FileA and FolderB are similar in name (both contain apples in the name somewhere) then move fileA to FolderB and so on.
Any help appreciated. Thanks!!!!
try Something like this
$PathWithFile="C:\temp\Test"
$PathWithDir="C:\temp\Test"
Get-ChildItem $PathWithFile -file -Filter "*.txt" | %{
$CurrentFile=$_
$Dirfounded=Get-ChildItem $PathWithDir -Directory | where {$_ -match $CurrentFile.BaseName} | select FullName -First 1
if ($Dirfounded -ne $null)
{
move-Item $CurrentFile.FullName -Destination $Dirfounded.FullName -WhatIf
}
}
A oneliner similar to #Esperento's
gci *.txt -af|%{$File=$_.FullName;gci "$($_.BaseName)*" -ad|%{Move $File -Dest $($_.FullName) -whatif}}
The verbose version:
PushD "X:\path\to\base\folder"
Get-ChildItem *.txt -File | ForEach-Object{
$File = $_.FullName
Get-ChildItem "$($_.BaseName)*" -Directory | ForEach-Object {
Move-Item $File -Destination $_.FullName -whatif
}
}
PopD
Both versions require PowerShell V3 for the -File and -Directory parameters (and their aliases -af/-ad) This can be substituted by an additional |Where-Object{ $_.PSIsContainer} respective | Where-Object{!$_.PSIsContainer}

Powershell - Compare Source and Target directories for .txt files. Copy all files that are not already in target to target AND a third directory

Need to compare source directory $source to $dest to see what .wav files are in source but not target. I want to copy those files that are not in target to the target AND to a third directory $sharefolder. Here is what I have so far. It appears that the copy-item is putting all of the differences into one copy string:
$sharefolder = '\\isilon1\blob\partial_dictation'
$comparefolder = '\\isilon1\blob\partial_dictation_compare'
$source = Get-childitem \\isilon1\blob\dcbld\pv -filter *.wav -recurse
$dest = Get-ChildItem \\isilon1\blob\Partial_Dictation_Compare -filter *.wav -recurse
$diff = Compare-Object $dest $source -passthru | Where-Object {$_.SideIndicator -EQ "=>"} | % {$_.FullName}
ForEach-Object {
Copy-Item "$diff" "$comparefolder" -WhatIf
Copy-Item "$diff" "$sharefolder" -WhatIf
}
This is what I get if I echo $diff ;
\\isilon1\blob\dcbld\pv\-D\CB\LD\-9\94\1.wav
\\isilon1\blob\dcbld\pv\-D\CB\LD\-9\94\2.wav
This is correct as these files are not in the destination. When the copy-item runs, it gives this:
Copy-Item : Cannot find path '\isilon1\blob\dcbld\pv-D\CB\LD-9\94\1.wav \isilon1\blob\dcbld\pv-D\CB\LD-9\94\2.wav' because it does not exist.
I am coming from Unix so this is new to me.
That's not quite how ForEach-Object works. You need to pipe objects to it, and it will iterate through them for you. You used it correctly the line above (% is an alias for ForEach-Object).
$diff | ForEach-Object {
Copy-Item "$_" "$comparefolder" -WhatIf
Copy-Item "$_" "$sharefolder" -WhatIf
}
See Get-Help ForEach-Object.
You could also use the foreach statement, which does a similar thing but isn't a cmdlet:
foreach ($file in $diff) {
Copy-Item "$file" "$comparefolder" -WhatIf
Copy-Item "$file" "$sharefolder" -WhatIf
}
See Get-Help about_foreach.
They function slightly differently and have their own benefits -- foreach being faster for small collections, and ForEach-Object accepting input from a pipe and being capable of streaming objects as soon as they enter the pipe -- but here they're basically identical.
Beware that Compare-Object just compares the name without path of file and directory objects, as far as I'm aware. Like it doesn't look at the file size or date, let alone content. That might be just fine for your needs, but it throws people, IMX.

How do I get only directories using Get-ChildItem?

I'm using PowerShell 2.0 and I want to pipe out all the subdirectories of a certain path. The following command outputs all files and directories, but I can't figure out how to filter out the files.
Get-ChildItem c:\mypath -Recurse
I've tried using $_.Attributes to get the attributes but then I don't know how to construct a literal instance of System.IO.FileAttributes to compare it to. In cmd.exe it would be
dir /b /ad /s
For PowerShell 3.0 and greater:
Get-ChildItem -Directory
You can also use the aliases dir, ls, and gci
For PowerShell versions less than 3.0:
The FileInfo object returned by Get-ChildItem has a "base" property, PSIsContainer. You want to select only those items.
Get-ChildItem -Recurse | ?{ $_.PSIsContainer }
If you want the raw string names of the directories, you can do
Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName
In PowerShell 3.0, it is simpler:
Get-ChildItem -Directory #List only directories
Get-ChildItem -File #List only files
Use
Get-ChildItem -dir #lists only directories
Get-ChildItem -file #lists only files
If you prefer aliases, use
ls -dir #lists only directories
ls -file #lists only files
or
dir -dir #lists only directories
dir -file #lists only files
To recurse subdirectories as well, add -r option.
ls -dir -r #lists only directories recursively
ls -file -r #lists only files recursively
Tested on PowerShell 4.0, PowerShell 5.0 (Windows 10), PowerShell Core 6.0 (Windows 10, Mac, and Linux), and PowerShell 7.0 (Windows 10, Mac, and Linux).
Note: On PowerShell Core, symlinks are not followed when you specify the -r switch. To follow symlinks, specify the -FollowSymlink switch with -r.
Note 2: PowerShell is now cross-platform, since version 6.0. The cross-platform version was originally called PowerShell Core, but the the word "Core" has been dropped since PowerShell 7.0+.
Get-ChildItem documentation: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem
A cleaner approach:
Get-ChildItem "<name_of_directory>" | where {$_.Attributes -match'Directory'}
I wonder if PowerShell 3.0 has a switch that only returns directories; it seems like a logical thing to add.
Use:
dir -r | where { $_ -is [System.IO.DirectoryInfo] }
From PowerShell v2 and newer (k represents the folder you are beginning your search at):
Get-ChildItem $Path -attributes D -Recurse
If you just want folder names only, and nothing else, use this:
Get-ChildItem $Path -Name -attributes D -Recurse
If you are looking for a specific folder, you could use the following. In this case, I am looking for a folder called myFolder:
Get-ChildItem $Path -attributes D -Recurse -include "myFolder"
Less text is required with this approach:
ls -r | ? {$_.mode -match "d"}
The accepted answer mentions
Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName
to get a "raw string".
But in fact objects of type Selected.System.IO.DirectoryInfo will be returned. For raw strings the following can be used:
Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | % { $_.FullName }
The difference matters if the value is concatenated to a string:
with Select-Object suprisingly foo\#{FullName=bar}
with the ForEach-operator the expected: foo\bar
Use:
dir -Directory -Recurse | Select FullName
This will give you an output of the root structure with the folder name for directories only.
You'll want to use Get-ChildItem to recursively get all folders and files first. And then pipe that output into a Where-Object clause which only take the files.
# one of several ways to identify a file is using GetType() which
# will return "FileInfo" or "DirectoryInfo"
$files = Get-ChildItem E:\ -Recurse | Where-Object {$_.GetType().Name -eq "FileInfo"} ;
foreach ($file in $files) {
echo $file.FullName ;
}
Use:
Get-ChildItem \\myserver\myshare\myshare\ -Directory | Select-Object -Property name | convertto-csv -NoTypeInformation | Out-File c:\temp\mydirectorylist.csv
Which does the following
Get a list of directories in the target location:
Get-ChildItem \\myserver\myshare\myshare\ -Directory
Extract only the name of the directories:
Select-Object -Property name
Convert the output to CSV format:
convertto-csv -NoTypeInformation
Save the result to a file:
Out-File c:\temp\mydirectorylist.csv
A bit more readable and simple approach could be achieved with the script below:
$Directory = "./"
Get-ChildItem $Directory -Recurse | % {
if ($_.Attributes -eq "Directory") {
Write-Host $_.FullName
}
}
Hope this helps!
My solution is based on the TechNet article Fun Things You Can Do With the Get-ChildItem Cmdlet.
Get-ChildItem C:\foo | Where-Object {$_.mode -match "d"}
I used it in my script, and it works well.
This question is well and truly answered but thought I'd add something extra as I've just been looking at this.
Get-ChildItem happens to produce two types of objects whereas most commands produce just one.
FileInfo and DirectoryInfo are returned.
You can see this by viewing the 'members' available to this command like so:
Get-ChildItem | Get-Member
TypeName: System.IO.DirectoryInfo
TypeName: System.IO.FileInfo
You'll see the various methods and properties available to each type. Note that there are differences. For example that the FileInfo object has a length property but the DirectoryInfo object doesn't.
Anyway, technically, we can return just the directories by isolating the DirectoryInfo object
Get-ChildItem | Where-Object {$_.GetType().Name -eq "DirectoryInfo"}
Obviously as the top answer states the most straightforward solution is to simply use Get-ChildItem -Directory but we now know how to work with multple object types in future :)
Use this one:
Get-ChildItem -Path \\server\share\folder\ -Recurse -Force | where {$_.Attributes -like '*Directory*'} | Export-Csv -Path C:\Temp\Export.csv -Encoding "Unicode" -Delimiter ";"
You can try the PsIsContainer Object
Get-ChildItem -path C:\mypath -Recurse | where {$_.PsIsContainer -eq $true}
To answer the original question specifically (using IO.FileAttributes):
Get-ChildItem c:\mypath -Recurse | Where-Object {$_.Attributes -band [IO.FileAttributes]::Directory}
I do prefer Marek's solution though:
Where-Object { $_ -is [System.IO.DirectoryInfo] }