I am trying to recursively get the files from a folder with multiple sub-folders. I noticed that Windows XP created this 5b....cb\amd64 files in the folder and it keeps throwing me the access denied error.
Get-ChildItem: Access to path is denied.
I do not have admin rights in these machines and what I am trying to do is skip these folders.
Is there a way to do this? Here is what I have tried with no success. Is it a good idea to suppress these messages as it does not break the script? Any help will be appreciated.
Get-ChildItem $sourceDir -Recurse | ? { $_.FullName -notlike '*\5b...cb\*'}
| Where-Object {$_.CreationTime.Date -eq ((Get-Date).Date)} |
foreach {
Write-Host $_
}
The error you see is thrown by Get-ChildItem, so trying to filter the inaccessible files further down the pipeline won't help. You need to suppress the original error using the ErrorAction common parameter:
Get-ChildItem $sourceDir -Recurse -ErrorAction SilentlyContinue | ...
Related
I am trying to search through folders on a share drive for subfolders with "*OutPuts" in the name. These folders should have folders in them but should not have files:
select fullname,#{N='SubDirectories';E={[boolean]($_ |
get-childitem -directory).count}},#{N='SubFiles';E={[boolean]($_ |
get-childitem -file).count}}
When these folders are found I want to move any folders from the output folder to the Project folder (Parent of its Parent). Example:
Starting Condition: C:\AllProjects\Projectfolder\outputs\SubProjectFolder
Wanted Outcome: C:\AllProjects\Projectfolder\SubProjectFolder
Here's the full Code:
code somewhat reformatted for readability ...
$folders=Get-ChildItem C:\AllProjects -recurse -Directory |
where {$_.name -like "*Outputs"} |
select fullname,
#{N='SubDirectories';E={[boolean]($_ |
get-childitem -directory).count}},
#{N='SubFiles';E={[boolean]($_ |
get-childitem -file).count}}
$folders
$folders |
where {$_.subdirectories -eq $true -and $_.subFile -eq $False} |
foreach {
get-childitem |
Move-Item $_ -destination $_.parent.parent}
When ever I run the code the following message comes up:
cmdlet Move-Item at command pipeline position 1
Supply values for the following parameters:
Path[0]:
I'm pretty new to PowerShell so if someone could help break this down for me and tell me what I'm supposed to put here that would be greatly appreciated. Thank you for your time.
UPDATE
*I've tried it with
-Path $_, -Path $_.PsPath, -Path $_.FullName.
I've even tried Get-Item. With the -Path though I now get a message that's a little different:
cmdlet ForEach-Object at command pipeline position 2 Supply values for the following parameters: Process[0]:
I am using the below code:
Get-ChildItem -Path N:\USERS -Filter DANTOM.DTM -Recurse -ErrorAction SilentlyContinue -Force
I need it to either find the file "DANTOM.DTM" or the extension ".DTM". I need to exclude the folder N:\USERS\EDI because it is a 1.7TB folder that would never have this file in it. So in doing so would really speed up the process.
I would like the end result to either spit into a .txt file saying which folders inside of N:\USERS has the file or just have it display as a list in powershell.
Thank you,
Assuming that the files of interest do not reside directly in N:\USERS (only in subdirs.), try the following (PSv3+ syntax); send to a file by appending > dirs.txt, for instance.
Get-ChildItem N:\USERS -Directory | ? Name -ne 'EDI' |
Get-ChildItem -Recurse -Filter *.DTM |
ForEach-Object { $_.DirectoryName }
Note: While it is tempting to try a simpler approach with -Exclude EDI, it unfortunately doesn't seem to be effective in excluding the entire subtree of the EDI subfolder.
So I want to clean up some of my users profiles on a nightly basis using the following "script"
Get-ChildItem 'F:\View\Profiles\' -Recurse -Force |
Where-Object {$_.Directory -match 'F:\\View\\Profiles\\((\w*).V2)\\AppData\\Roaming\\Trillian\\users\\((\w*%\w*%\w*))\\logs\\(_CLOUD|ASTRA)'} |
Remove-Item -Recurse -Force
This deletes all the log files but leaves behind the folder structure. Because of that I tried the following query.
Get-ChildItem 'C:\TestProfileClean' -Recurse -Force |
Where-Object {(($_.Name -eq "logs" -and $_.Directory) -and $_.DirectoryName -match 'C:\\TestProfileClean\\(\w*.V2)\\AppData\\Roaming\\Trillian\\Users\\((\w*)%40(\w*)%2Ecom)' )} |
Remove-Item -Recurse -Force
This ensures that the folder deleted is named "logs" it is a directory and the directory name is in this format.
C:\\TestProfileClean\\(\w*.V2)\\AppData\\Roaming\\Trillian\\Users\\((\w*)%40(\w*)%2Ecom)
The problem is when I run the second command it does not delete any files, if I cut off $_.DirectoryName then I get the following output.
Directory: C:\TestProfileClean\username.V2\AppData\Roaming\Trillian\users\username%40compcorp%2Ecom
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 12/14/2016 9:56 AM logs
Which will delete what I want but isn't specific enough for me to be comfortable running it.
What am doing wrong that is preventing folders from being deleted but the script will delete all non folder items.
You are missing the wildcard to go through all of them under the folder . I just modified your code :
Get-ChildItem 'F:\View\Profiles\*' -Recurse -Force |
Where-Object {$_.Directory -match 'F:\\View\\Profiles\\((\w*).V2)\\AppData\\Roaming\\Trillian\\users\\((\w*%\w*%\w*))\\logs\\(_CLOUD|ASTRA)'} |
Remove-Item -Recurse -Force
Note: I am not checking the Regex. I am only tweaking the get-childitem part.
Apart from this , I have 2 other suggestion for you. If you really want to delete the user profiles nightly basis, then you can use the small utility tools
1) made of batch
2) made of powershell.
Below are the links for your reference.
Delprof - Batch
Delprof - Powershell
Hope this helps...!!!
I am trying delete all files within a folder but there is 1 folder called pictures which I would like to keep but don't know how to do that. I am using the following script , it deletes everything in a folder
if ($message -eq 'y')
{
get-childitem "C:\test" -recurse | % {
remove-item $_.FullName -recurse
}
}
One solution is to use something like:
Get-ChildItem -Path "c:\test" -Recurse | Where-Object { $_.FullName -cnotmatch "\\Pictures($|\\)" -and (Get-ChildItem $_.FullName -Include "Pictures" -Recurse).Length -eq 0 } | Remove-Item -Recurse -ErrorAction SilentlyContinue;
I suspect there must be a way more elegant way to do this. Here's what this does: it enumerates all files in the C:\test folder recursively (Get-ChildItem), then it removes all items from the result list using Where-Object where the path contains the directory to be excluded (specified using regex syntax) or when the item in question has child items that contains the file or directory to be excluded. The resulting list is fed to Remove-Item for removal. The -ErrorAction SilentlyContinue switch is applied to prevent errors being logged with recursive removal.
Get-ChildItem $PSScriptRoot -Force| Where-Object {$_.Name -ne "Pictures"} | Remove-Item -Recurse
I just tried this, and it worked for me. If you want to change what is deleted just change the "Pictures". This uses $PSScriptRoot for the path, which is the execution path of the Powershell script. You can rename that to be the path of where you want to delete.
I am not understanding what is happening.
I am attempting to copy and paste dll's from one directory and another.
gci -path $FromPath -Include ("*.dll", "*.pdp") | ? {$_.Name -match "appMaskA|appMaskB|appMaskC"} | foreach{Copy-item $_.Fullname -destination $ToPath -force}
Now that command works for one function that I have it in, but not for this one...
Now, this command is moving dll's to a different server. Not certain why it isn't working.
And if it isn't working it should throw an exception. I did wrap that command in a try catch by the way? Should I be catching a specific exception?
What does your $ToPath look like? If your code is wrapped in try/catch add -ErrorAction Stop parameter to your copy statement as the default value is to continue so the catch block will never be executed.
gci -path $FromPath -Include ("*.dll", "*.pdp") | ? {$_.Name -match "appMaskA|appMaskB|appMaskC"} | foreach{Copy-item $_.Fullname -destination $ToPath -force -ErrorAction Stop}
Does this need to be Powershell or can you use XCOPY via a BASH/CLI script. Using XCOPY you can access C Drive by doing
SERVER.DOMAIN.LOCAL/c$/path/to/dll
Maybe this works for you:
gci -path $FromPath -Include *.dll,*.pdp | where {$_.Name -match "appMaskA|appMaskB|appMaskC"} | Copy-item -path $_ -destination $ToPath -force
For complicated and/or large copying jobs I would use the program robocopy. Robocopy is part of Windows. Execute this command to verify its location:
Get-Command robocopy|select path