Delete long name path folders older than 7 days - path

I need to make a script that it will be delete folders and files(all things), but only older than 7 days. I have a code, but it has a problem when path name is longer than 256 chars. One of the user has a many folders, name lenght equal 303 chars.
Below code works on Windows 10, but has problem on Windows 2008R2(.net 4.7.2) with \\?\....
Get-ChildItem -Path '\\?\c:\test\users' | ?{ $_.CreationTime -le $(Get-Date).AddDays(-7) } | Remove-Item -recurse -force
And that has problem with long path name:
Get-ChildItem D:\foler\folder\file | ?{ $_.CreationTime -le $(Get-Date).AddDays(-7) } | Remove-Item -Force -Recurse
Get-ChildItem D:\foler\folder\file | ?{ $_.CreationTime -le $(Get-Date).AddDays(-7) } | Remove-Item -Force -Recurse
Also I try with robocopy. So will be ok any script on powershell, .bat or .vbs.

Related

I want to copy folder EXCLUDING those with certain folder names

I have a folder called "Testnames" and within that two folders called "Bunny, Bugs_" and "Lightyear, Buzz".
Using the command:
Get-ChildItem C:\Users\Galen\Desktop\Testnames -Recurse -Exclude '_'
I get:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 10/9/2015 11:43 AM Lightyear, Buzz
Perfect!
However when I use the command:
Copy-Item C:\Users\Galen\Desktop\Testnames -Exclude '_' -Recurse C:\Users\Galen\Testfolder
Both Bug's and Buzz's folders are copied into Testfolder. What change do I need to implement to only copy Buzz's folder?
Run Get-ChildItem without the parameter -Recurse to get the root folders. Filter that list for items that don't have an underscore in their name. Then copy the remaining items recursively.
Get-ChildItem 'C:\Users\Galen\Desktop\Testnames' |
Where-Object { $_.Name -notlike '*_*' } |
Copy-Item -Destination 'C:\Users\Galen\Testfolder' -Recurse

Powershell - query servers with roles installed

I have a txt file with all the server names.
I want to query each server in the txt to see if RDS is installed. So far it works fine. But the export needs to have servername and the installed role. how can I pass the servername to the output file.
At the moment it is querying but only the installed. so with a 100 servers who really can tell which server has it installed and which doesnt>>>> PLEase help
Import-Module ServerManager
Get-Content W:\AllWindows.txt | ForEach-Object {Get-WindowsFeature -Name Remote-Desktop-Services} | Format-Table -Auto -wrap | Out-File -FilePath $csvfile
Try something like this:
Get-Content W:\AllWindows.txt | ForEach-Object { #(($_), (Get-WindowsFeature -Name Remote-Desktop-Services)) } | Format-Table -Auto -wrap | Out-File -FilePath $csvfile
I have worked it out:
Get-Content W:\windows2012.csv | Foreach-Object {{Get-WindowsFeature | where-object {$_.Installed -eq $True} | Export-Csv -Path "W:\output\$_.txt"}

PowerShell copy random files

I am trying to copy 7 random .txt files to a different location, but sub-folders get copied instead of the .txt files.
Here is my script:
$d = #(gci G:\Users\Public\Test) | resolve-path | get-random -count 2
$d | gci | get-random -count 7
Copy-Item $d -destination G:\Users\Public\Videos
What do I need to change?
One possible solution might be to use the PSIsContainer attribute to filter out folders.
I tried the following...
$d = gci "C:\Work\a\*.txt" | Where {$_.psIsContainer -eq $false}| resolve-path | get-random -count 7
Copy-Item $d -destination C:\Work\b
The where clause filtered out anything that was not a "container" and ignored the test folders I had set up. If you need .txt files specifically then use the wildcard included in the path as above.
Also, if you were to add -recurse then it would presumably search in all sub folders of your original search location and still filter out any "folders" for copying. Though I haven't tested this very thoroughly.
$d = gci "C:\Work\a\*.txt" -recurse | Where {$_.psIsContainer -eq $false}| resolve-path | get-random -count 7

What is wrong with my script? Deletes all files -- Powershell

I want to delete files recursively, while keeping the seven most recent ones. The following script deletes all files, even though I specify the skip parameter.
$files = gci -path f:\inbox\test -recurse | where {!$_.PsIsContainer}
foreach ($file in $files) {sort creationtime -desc | select -skip 7 | remove-item -path $file.FullName -force}
This script doesn't do a damn thing:
gci -path f:\inbox\test -recurse | where {!$_.PsIsContainer} | foreach-object {sort creationtime -desc | select -skip 7 | remove-item -force }
try this
$files = gci -path f:\inbox\test -recurse | where {!$_.PsIsContainer} | sort creationtime -desc | select -skip 7
foreach ($file in $files) {remove-item -path $file.FullName -force}
or more simply
gci -path f:\inbox\test -recurse | where {!$_.PsIsContainer} | sort creationtime -desc |
select -skip 7 | remove-item -force

Powershell: Count items in a folder with PowerShell

I'm trying to write a very simple PowerShell script to give me the total number of items (both files and folders) in a given folder (c:\MyFolder). Here's what I've done:
Write-Host ( Get-ChildItem c:\MyFolder ).Count;
The problem is, that if I have 1 or 0 items, the command does not work---it returns nothing.
Any ideas?
You should use Measure-Object to count things. In this case it would look like:
Write-Host ( Get-ChildItem c:\MyFolder | Measure-Object ).Count;
or if that's too long
Write-Host ( dir c:\MyFolder | measure).Count;
and in PowerShell 4.0 use the measure alias instead of mo
Write-Host (dir c:\MyFolder | measure).Count;
I finally found this link:
https://blogs.perficient.com/microsoft/2011/06/powershell-count-property-returns-nothing/
Well, it turns out that this is a quirk caused precisely because there
was only one file in the directory. Some searching revealed that in
this case, PowerShell returns a scalar object instead of an array.
This object doesn’t have a count property, so there isn’t anything to
retrieve.
The solution -- force PowerShell to return an array with the # symbol:
Write-Host #( Get-ChildItem c:\MyFolder ).Count;
If you need to speed up the process (for example counting 30k or more files) then I would go with something like this..
$filepath = "c:\MyFolder"
$filetype = "*.txt"
$file_count = [System.IO.Directory]::GetFiles("$filepath", "$filetype").Count
Only Files
Get-ChildItem D:\ -Recurse -File | Measure-Object | %{$_.Count}
Only Folders
Get-ChildItem D:\ -Recurse -Directory | Measure-Object | %{$_.Count}
Both
Get-ChildItem D:\ -Recurse | Measure-Object | %{$_.Count}
You can also use an alias
(ls).Count
Recursively count files in directories in PowerShell 2.0
ls -rec | ? {$_.mode -match 'd'} | select FullName, #{N='Count';E={(ls $_.FullName | measure).Count}}
In powershell you can to use severals commands, for looking for this commands digit: Get-Alias;
So the cammands the can to use are:
write-host (ls MydirectoryName).Count
or
write-host (dir MydirectoryName).Count
or
write-host (Get-ChildrenItem MydirectoryName).Count
To count the number of a specific filetype in a folder.
The example is to count mp3 files on F: drive.
( Get-ChildItme F: -Filter *.mp3 - Recurse | measure ).Count
Tested in 6.2.3, but should work >4.

Resources