Reputation: 175
How I can do a ls
using PowerShell?
for i in `ls`
do
if [ -d $i ] #miro si és directori
then
echo "Directory"
else echo "File"
fi
done
POWERSHELL
$llistat -ls
forEach $element in $llistat ??? this is possible
}
Upvotes: 0
Views: 181
Reputation: 175084
In PowerShell, the Get-ChildItem
cmdlet works like ls
(at least with the file system provider). All items returned have a PowerShell-specific property called PSIsContainer
, indicating whether it's a directory or not:
foreach($item in (Get-ChildItem)){
if($item.PSIsContainer){
"Directory"
} else {
"File"
}
}
If you want to see what's inside each directory, one level down:
foreach($item in (Get-ChildItem)){
if($item.PSIsContainer){
# Directory! Let's see what's inside:
Get-ChildItem -Path $item.FullName
}
}
As of PowerShell version 3.0 and up, the Get-ChildItem
supports a File
and Directory
switch on the filesystem provider, so if you ONLY want directories, you could do:
Get-ChildItem -Directory
So the second example becomes:
Get-ChildItem -Directory | Get-ChildItem
You could also list files recursively (like ls -R
):
Get-ChildItem -Recurse
Upvotes: 0
Reputation: 200573
A more PoSh way is to use a pipeline, and perhaps a hashtable:
$type = @{
$true = 'Directory'
$false = 'File'
}
Get-ChildItem | ForEach-Object { $type[$_.PSIsContainer] }
PowerShell even has a default alias ls
for Get-ChildItem
, so you could use more Unix-ish syntax:
ls | % { $type[$_.PSIsContainer] }
Upvotes: 1