I'm trying to make a PowerShell script that checks a set registry key for a range of names that start the same. That part I have working fine. I also need this script to than remove those items from that registry and I am having trouble remembering how to pass the names of all items I find so that Remove-ItemProperty will work. This is what I have.
$Reg = 'HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Devices'
Get-ItemProperty -Path $Reg | Select-Object IS* | ForEach-Object {$PSItem.Name} | Remove-ItemProperty -Path $Reg -Name $name
The message I get is that Name is null so I'm not storing the names correctly. They display correctly if I just run the first two pipes.
Try this. Had to re-write a bit to make the property name stick.
Get-Item -Path "$Reg" | Select-Object -ExpandProperty Property |
ForEach-Object {if ($_ -match "IS*"){Remove-ItemProperty -Path "$Reg" -Name "$_"}}
Related
I have a long list of folders. Most of the folders follow the "name_#name" format. I have some that don't follow that structure. I want to move all the folders (and the sub-folders/files within) that DON'T have "_" in the folder name.
For example:
test_#12352
moose_#4532
horse_#84462
cow24
fish3
Moved:
cow24
fish3
I think Move files when they contain a specific word? could be modified to make it work...just not sure how. I'm used to just using GUI, this is my first time using PowerShell
When I tried using that code in that link it didn't work with my situation
What you want to do is just filter the list before you move any files
So you can use the following to pick up all the files you want
$Files = Get-childItem -Path $Path -File
You can then filter it down. My favourite way is to pipe the variable into Where-Object and play around with the individual properties and match types. Since you don't want to include the _ we can use a -notmatch "_" to exclude those values
$Files = Get-ChildItem -Path $Path -File | Where-object{$_.Name -notmatch "_"}
And finally, you can move the files
$Files | move-item -path $_.FullName -Destination $Destination
Or as a one liner
Get-ChildItem -Path $Path -File | Where-object{$_.Name -notmatch "_"} | move-item -path $_.FullName -Destination $Destination
*Please note I haven't really tested this code. So test it out yourself before you run it
I am able to batch rename files in a working directory by using:
Dir | %{Rename-Item $_ -NewName ("0{0}.wav" -f $nr++)}
However I want the file rename to start at something other than zero. Say 0500, and rename sequentially in order.
Dir | %{Rename-Item $_ -NewName ("0{500}.wav" -f $nr++)}
returns error.
How can I tell rename to start at a number other than 0?
You can initialize the counter beforehand to 500. Also, you don't need to use a ForEach-Object loop (%) for this, because the NewName parameter can take a scriptblock.
Important here is that you need to put the Get-ChildItem part in between brackets to let that finish before renaming the items, otherwise you may end up trying to rename files that have already been renamed.
$nr = 500
(Get-ChildItem -Path 'D:\Test' -Filter '*.wav' -File) | Rename-Item -NewName { '{0:D4}{1}' -f ($script:nr++), $_.Extension }
I'm using PowerShell script to delete folders which are older than x days.
$limit = (Get-Date).AddDays(-15)
$path = "xxxx\path"
# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse`
But it doesn't work always, and I couldn't figure out what's wrong. is there any other way I can delete certain folders for example if there are more then 5 folders..? or is it best to delete which one is older..?
I do a backup via Powershell script which works great, but when I'm using this script to delete older backups some reason it doesn't work always.
is there any alternative way to do this.?
Lot's of love humans
#Alex_P thanks, it's confusing but after changing the path for scripts they seem to be working. before path was like ( Users/admin/folder test/script.p1) I changed it to ( Users/admin/folder-test/script.p1)
after changing it solved the problem, still, don't know why it had happened.
Thanks all
I guess "Users/admin/folder test/script.p1" should have worked. There is a blank in the path. Therefore, "" are needed
I have a number of folders that I am trying to copy. The folders are all name 'yyyymmdd' i.e. 20190615, going back up to a year or more. I am trying to figure out a way to copy only the last 45 days of these folders. The biggest issue that I've run into is that the computers that I am running this on only have Powershell 2.0, which seems to have some limitations that 5 or greater does not have.
I have been able to get the list of all of the folders in the path with :
$datedsubs = Get-ChildItem -path $path | where-object { $_ -like "20*" }
From there though, I am a little stuck. I feel this would be easier with PS 5 or greater. I've tried Robocopy, even though that is not a PS solution, but that copies everything, and I just want the folders.
I've tried something like the following, but it doesn't seem to work in PS 2.0.
Get-ChildItem -Path $path | Where-Object { ($_ -like '20*') -and ($_.LastAccessTime -lt $datedlimit)} | Copy-Item -Destination $destination -Recurse
Any help would be appreciated here.
Thanks
Putting what Lee has said into some code.
Get-ChildItem -Path $Path -Filter "20*" | Where-Object {($_.PSIsContainer) `
-and ($_.LastAccessTime -gt ((Get-Date).AddDays(-45)))} | `
Copy-Item -Destination $destination -Recurse
This should get you what you are after.
I have put it to -gt 45 days ago because this will only show folders accessed within the last 45 days.
I have about 700 .txt files scattered in 300 directories and sub-directories.
I would like to open each of them, convert all text inside to lowercase, including Unicode characters (such as É to é), then save and close them.
Can you advise how it could be done through PowerShell? It is my own computer and I have admin rights.
I have started with the below:
Get-ChildItem C:\tmp -Recurse -File | ForEach-Object {}
but I am not sure what to put between the brackets of ForEach-Object {}.
Simple script, with your requirements:
$path=".\test\*.txt"
#With Default system encoding
Get-ChildItem $path -Recurse | foreach{
(Get-Content $_.FullName).ToLower() | Out-File $_.FullName
}
#Or with specified encoding
Get-ChildItem $path -Recurse | foreach{
(Get-Content $_.FullName -Encoding Unicode).ToLower() |
Out-File $_.FullName -Encoding Unicode
}
#Test
Get-ChildItem $path -Recurse | foreach{
Write-Host "`n File ($_.FullName): `n" -ForegroundColor DarkGreen
Get-Content $_.FullName
}
You need to use :
# Reading the file content and converting it to lowercase and finally putting the content back to the file with the same filename.
(Get-Content C:\path\file.txt -Raw).ToLower() | Out-File C:\path\file.txt -Force
inside the foreach and then change the case to lower.
If you want to iterate all the files in the corresponding folder then you can use another foreach to get that job done.
Hope it helps.