I'm trying to get all the ERROR and WARN lines to files with the name of an object, like the following:
$items = Get-ChildItem -Path "C:\Test"
foreach ($item in $items)
{
gci $item *.log* -rec -Force -ErrorAction SilentlyContinue |
Select-String "WARN|ERROR" >> "C:\MyDir$item.Name_logTest.txt"
}
However, the recursive flag doesn't allow me to go over all folders.
EDIT: Guys, I figured it out. Instead of using "gci $item", I used "gci $item.PSPath"
ForEach ($Item in (Get-ChildItem -Path "C:\Test" -filter "*.log" -recurse)) {...
Related
I have the following code
$top = Get-ChildItem -File C:\SomeFolder\1\ -Recurse -filter *.evtx
foreach ($file in $top) {
$get = (Get-WinEvent -Path C:\SomeFolder\1\$file -erroraction 'silentlycontinue').count
if ($get -eq '0') {
Remove-Item -Path C:\SomeFolder\1\$file -Recurse -Force
}
}
The problem is that the files that I'm trying to delete are in use by the loop.
The following error appears - "Remove-Item : Cannot remove item The process cannot access the file because it is being used by another process."
I even tried exiting with break but it didn't help.
Any suggestions?
Thanks!
I have a bunch of *.FLAC files in different subfolders, which I want to turn into zero byte files, for database use only. I have the following code, but it makes all files to zero bytes. I don‘t know, how to fit the code. Maybe you can help? Thanks!
$Path='G:\Music\'
$Files=Get-ChildItem -Path $Path -File -Recurse | Where-Object {($_.Length) -gt 0}
ForEach ($File in $Files) { New-Item$File.FullName -Force -Value '' }
This is how you can replace all your FLAC files for zero byte FLAC files recursively. You can use a normal foreach loop or pipe to foreach-object.
You don't really need to check if the FLAC file's length is greater than 0 but you can also add that condition.
$Path='G:\Music' # Define parent path
$Files=Get-ChildItem -Path $Path -File -Recurse -Filter *.FLAC # Get all FLAC files recursively
foreach($file in $files)
{
$null > $file.FullName
}
# OR using foreach-object (same result)
Get-ChildItem -Path $Path -File -Recurse -Filter *.FLAC | ForEach-Object {$null > $_.FullName}
Edit:
Adding these 2 examples below for PS 5.1. As it seems, $null > file doesn't create an actual zero byte file on this PS version, see below comments from #mklement0 for more info.
# Example 1 (Not efficient)
Get-ChildItem -Path $Path -File -Recurse -Filter *.FLAC | ForEach-Object {
New-Item -Path $_.FullName -Force
}
# Example 2
New-Item -Force (Get-ChildItem -Path $path -File -Recurse -Filter *.FLAC).FullName
I'm trying to delete a folder out of the \AppData\Local\Microsoft_Corporation directory for all users on a given computer. I found a few PowerShell script that can complete this task for me but extra wrinkle here is that this folder name is slightly different for every user. The folder name I'm trying to remove looks like this - harmony_Path_lzm5ceganmb1ihkqq2. It always has the word "harmony" in the folder name, so I'm trying to search for any folder with this keyword and remove it.
This is the script I have so far:
$users = Get-ChildItem C:\Users
foreach ($user in $users){
$folder = "$($user.fullname)\AppData\Local\Microsoft_Corporation\*"
If (Test-Path $folder) {
Remove-Item $folder -Recurse -Force -ErrorAction silentlycontinue -WhatIf
}
}
This seems to work fine to remove every folder in \AppData\Local\Microsoft_Corporation\ but when I try to search for "harmony" keywords with the Where-Object Cmdlet. I can't get it to work correctly.
$users = Get-ChildItem C:\Users
foreach ($user in $users){
$folder = "$($user.fullname)\AppData\Local\Microsoft_Corporation\* | Where-Object {$_.Name -like "*harm*"}"
If (Test-Path $folder) {
Remove-Item $folder -Recurse -Force -ErrorAction silentlycontinue -WhatIf
}
}
Can anyone help me with this?
$users = Get-ChildItem C:\Users
foreach ($user in $users){
$folder = "$($user.fullname)\AppData\Local\Microsoft_Corporation\*Harmony*"
If (Test-Path $folder) {
Remove-Item $folder -Recurse -Force -ErrorAction silentlycontinue -WhatIf
}
}
$folder contains a string - path. It does not contains list of files to use Where-Object Cmdlet.
Another way:
Get-ChildItem "C:\Users\*\AppData\Local\Microsoft_Corporation\*harmony*" -Directory | Remove-Item -WhatIf
why did you put where-object inside " " ? powershell read this as a string
try using this:
$users = Get-ChildItem C:\Users
foreach ($user in $users){
$folder = "$($user.fullname)\AppData\Local\Microsoft_Corporation\"
If (Test-Path $folder) {
Get-ChildItem $folder -Recurse | Where-Object {$_.Name -like "*harm*"}|Remove-Item -Force -ErrorAction silentlycontinue
}
}
I've got the following powershell line:
$items = Get-ChildItem -Path "C:\temp" -Filter "*ä*" -Recurse | Select FullName
I want to write those paths to a *.txt file:
foreach ($item in $items){
add-content -Path C:\temp\test.txt -Value $item -Force
}
Output in test.txt:
#{FullName=C:\temp\ä}
How can I only write the Value of FullName to the txt-file ?
For example:
echo $item
and Output in Console:
C:\temp\ä
Common gotcha with PowerShell. $items is not an array of FullNames but an object array with a fullname property. (Array assuming more that one item was returned.)
$items = Get-ChildItem -Path "C:\temp" -Filter "*ä*" -Recurse | Select -Expand FullName
or
# Requires PowerShell v3.0 or above
$items = (Get-ChildItem -Path "C:\temp" -Filter "*ä*" -Recurse).Fullname
You should be able to do this:
foreach ($item in $items){
add-content -Path C:\temp\test.txt -Value $item.FullName -Force
}
Here's a common way to find this bit of info:
Get-ChildItem | Get-Member
Or the same with shorthands:
dir | gm
This will list all properties, methods, etc for the objects returned from Get-ChildItem.
I've got the following code snippet which currently removes everything in my temp directory and re-adds a new temp directory.
if($serverVersion.name -like "*2003*"){
$dir = "\\$server" + '\C$\WINDOWS\Temp\*'
remove-item $dir -force -recurse
if($?){new-item -path "\\$server\admin$\Temp" -Type Directory}
}
elseif($serverVersion.name -like "*2008*"){
$dir = "\\$server" + '\C$\Windows\Temp\*'
remove-item $dir -force -recurse
if($?){New-Item -Path "\\$server\admin$\Temp" -Type Directory}
}
I'm trying to slightly alter the code to where it will no longer delete the temp directory and instead simply remove all of the contents inside of temp. I added \* at the end of my $dir variable so that it tries to get all of the items inside of temp rather than deleting Temp itself. When I run this however I'm not deleting anything. What is wrong with my code?
This works for me, so long as you meet the pre-reqs and have full control over all files/folders under Temp
# Prerequisites
# Must have the PowerShell ActiveDirectory Module installed
# Must be an admin on the target servers
#
# however if you have no permissions to some folders inside the Temp,
# then you would need to take ownship first.
#
$Server = "server Name"
$dir = "\\$Server\admin$\Temp\*"
$OS = (Get-ADComputer $Server -Properties operatingsystem).operatingSystem
IF (($os -like "*2003*") -or ($os -like "*2008*"))
{
remove-item $dir -Recurse -force
}
According to the PowerShell help file for remove-item, the -recurse parameter is faulty. It recommends that you get-childitem and pipe to remove-item. See example from the help file below.
-------------------------- EXAMPLE 4 --------------------------
C:\PS>get-childitem * -include *.csv -recurse | remove-item
Figured out how to do this and figure it may be useful for someone in the future.
if($serverVersion.name -like "*2003*"){
$dir = "\\$server" + '\C$\WINDOWS\Temp'
Get-ChildItem -path $dir -Recurse | %{Remove-Item -Path $_.FullName -Force}
if($?){new-item -path "\\$server\admin$\Temp" -Type Directory}
}
elseif($serverVersion.name -like "*2008*"){
$dir = "\\$server" + '\C$\Windows\Temp'
Get-ChildItem -path $dir -Recurse | %{Remove-Item -Path $_.FullName -Force}
write-host "success?"
if($?){New-Item -Path "\\$server\admin$\Temp" -Type Directory}
}
Using get-childitem it will look at everything inside of Temp without deleting Temp itself.