Cannot use Get-ChildItem to list filesnames in powershell script - powershell

In the powershell command-line I can execute the following
$list = Get-ChildItem $path -name
foreach($k in $list){Write-host $k}
And it will list all the filenames in $path
But if I copy and paste the same thing in code to execute, the output is blank
Why would this even happen?

Write-Host will write the string directly to the Host aka. the command line window, nowhere else
Just replace Write-Host with Write-Output if you want $k written to STDOUT:
$list = Get-ChildItem $path -name
foreach($k in $list){Write-Output $k}
But in reality, that is unnecessary, Write-Output is called implicitly. You could achieve the same thing with just:
Get-ChildItem $path -Name

I wanted just a list of files sorted by size without directory headers. I used this to accomplish it.
get-childitem foldername -file -recurse | select FullName,Length | sort Length -descending | export-csv c:\export.csv

Related

get-childitem variable return empty value when running is powershell script

$NewestFile = $NULL
$path = "C:\myDirectory"
$NewestFile = Get-ChildItem -filter “FileName*.xlsx” -recurse -path $path |
Sort LastWriteTime -Descending |
Select -First 1
Write-Host " NewestFile" $NewestFile
I have several files with nearly the same name and I want to select the newest file for processing. When I run this on the command line it works fine but the same code will not run from a powershell script.
Looks good except the Write-Host. If you want a property of the $NewestFile object such as full path or some such, use $NewestFile.FullName

Delete a list of folders in temp

I want to delete a list of folders in temp.
Name of folders are numeric- 2, 3, 4 etc..
PS C:\Users\sos$> Get-ChildItem -path C:\Temp
Is there a PowerShell way to get this scripted
Soheil Hashemi opened the bag on answering this question so lets have a look at a basic option.
You need to have all the files that are just a number from the temp directory. Let use Where-Object to match the files we are looking for.
$Path = "C:\temp"
Get-ChildItem $Path -Directory | Where-Object{$_.BaseName -notmatch "\D"} | Remove-Item -Confirm:$false -WhatIf
Get all the files from the $path and match any files where the file name (without extension) only contains numbers. Then pipe the results into Remove-Item. Remove the -WhatIf when you are sure it is finding the right files.
If you don't have PowerShell 3.0 then you can change the Where clause and remove the -Directory switch.
Get-ChildItem $Path | Where-Object{$_.BaseName -notmatch "\D" -and $_.PSIsContainer} | Remove-Item -Confirm:$false -WhatIf
your question is not complete but first create list like me
$a = #'
1.txt
2.txt
3.txt
4.txt
'#
or $a = (get-content c:\filelist.txt)
then
$a | foreach { Remove-Item c:\temp\$_ }

Trying to remove-item but it's not getting removed

I'm thinning out my backup files with a powershell script, and I know I have the correct filenames, but for some reason when I use remove-item, the item doesn't get removed and no exception is thrown. This is what it looks like:
try{
$Drive = "E:\temp\"
$deleteTime = -42
$limit = (Get-Date).AddDays($deleteTime) #files older than 6 weeks
#get files in folder older than deleteTime and with signature of *junk.vhd* (to be changed later)
$temp1 = Get-ChildItem -Path $Drive -filter "*junk.vhd*" | Where-Object {$_.LastWriteTime -lt $limit} | Select -Expand Name #this has 5 files in list
#will delete every other file
for($i=$temp1.GetLowerBound(0);$i -le $temp1.GetUpperBound(0);$i+=2) {
$name = $temp1[$i]
Write-Host "removing $name" #prints correct file names to screen
Get-ChildItem -Path $Drive -include $name | Remove-Item -recurse -force #this is handling correct files but they aren't deleted for some reason
}
}#try
Catch [Exception] {
#nothing is caught
Write-Host "here"
}
Does anyone have any ideas why it's finding and Write-Host the correct filenames to remove, but the Remove-Item isn't removing them?
I was looking at removal a little different example, but everything looks good.
try to replace this line:
Get-ChildItem -Path $Drive -include $name | Remove-Item -recurse -force #this is handling correct files but they aren't deleted for some reason
with this:
Remove-Item $temp1[i].FullName -force -recurse
Since you already got the full patrh to the file, I don't feel it's necessary to call Get-ChilItem again and pass it through the pipeline, instead you just feed Remove-Item with the FUllName property which is the full path to thew file.

Writing list of ChildItem for a list of paths to a CSV file

What I am attempting to do is have an end CSV file like this:
path , get-childItem
path , get-childItem
path , get-childItem
etc.
I am very new to powershell so as of right now the issue I am having isn't with errors but rather actually knowing what to do
Here is my code thus far:
$data = gc "c:\Documents and Settings\user\Desktop\paths.txt"
ForEach($line in $data) {
$subName = Get-ChildItem "$line"
write-host $line","$subName
#Export-Csv -Path "C:\Documents and Settings\reidli\Desktop\mytest.csv" -Force "true" -InputObject $output
So I am reading a list of paths from paths.txt where each line is a different path
$subName is the list of Children i want and is correct
and when I write-host the output is in fact: path,children
commentented out is where I attempted to export csv, but this wont work as it would over-write the existing csv for each path
Can someone please help me take each line in the foreach loop and create a .csv file with each path and children?
just don't use export-csv at all. CSV is plain comma separated text file. which makes it easy to export:
$data = gc "c:\Documents and Settings\user\Desktop\paths.txt"
Remove-Item "C:\Documents and Settings\reidli\Desktop\mytest.csv" -ea 0
ForEach($line in $data) {
$subName = Get-ChildItem "$line"
write-host $line","$subName
"$line,$subName" | Add-Content "C:\Documents and Settings\reidli\Desktop\mytest.csv"
}
btw. do you really want the sub files space separated? it is what you get when you convert Get-ChildItem to string
Try this:
$data | select $_ , #{L="subname";E=#{(gci $_)}} | Export-Csv "C:\Documents and Settings\reidli\Desktop\mytest.csv" -force

powershell script delete folders regular expression

I've written a Powershell script to delete subfolders within a given folder whose name starts with either 0 or 1. This script seems to work only for non-empty folders. I want it to delete the inner contents too. Is there any switch that makes it possible? Also, for some files, I get an error about not having enough permissions whereas the script runs as an administrator.
$srcFolder = "C:\Documents and Settings\setup\Desktop\Temp\"
$folderList = Get-Childitem $a | Where-Object {$_.Name -match "^[01]"}
foreach( $folder in $folderList)
{
$tempFolder = $srcFolder + $folder;
Remove-Item $tempFolder
}
Any ideas?
Regards,
Sujeet Kuchibhotla
You can simplify this quite a bit:
Get-ChildItem $srcFolder | Where {$_.PSIsContainer -and ($_ -match '^[01]')} |
Remove-Item -Recurse -WhatIf