I am trying to make a Powershell script that will display a list of files and folders in a directory('s) while not displaying a specific folder and a file.
I have a bunch of directories on the C:\ that have a folder "DATA" and a "Setup.ini" file within them.
Every once in a while people go into the data folder and make copies and then place those copies of files and directories right inside of the main directory with the "DATA" and ".ini" files.
Then at the end of each month, I normally need to go through each of these directories and delete anything that is not the "DATA" folder and ".ini" file.
Get-ChildItem -Recurse "C:\TestDir" -Exclude 'Setup.ini' | Where-Object { $_.FullName -notmatch 'Data'}
This seems to sort of work and is what I have been using this month.
It runs really slow. I also noticed that one of the Directories had 2 extra folders and a zip file within it.
The script only showed one of the folders and didn't show the zip file either.
I have tried adding other things to this code, but when I do that it begins showing me data inside of the "DATA" folder which I don't want.
Any idea what I could be doing wrong or ways to make this run quicker?
I think it is taking about 7-10mins to run through 113 directories.
Thank you!
Expand the -Exclude and drop the Where-Object:
Get-ChildItem -Recurse "C:\TestDir" -Exclude 'Setup.ini','Data'
HTH
Related
I am trying to copy music lyric files (.lrc) from a folder with many subfolders such as artists and albums and paste them in an empty folder, but I still want to keep the folder structure without having to create each individual folder for those files to be put in. In other words: I want to take certain files from a folder and have it automatically create the folder structure in another folder which the original file was in.
For example: I have 10 lyric files accompanied with other music files in a single folder called "ArtistName" which is a subfolder of a subfolder of a folder called "Music". These lyric files need to be in another folder called "Music2" that is currently empty, but instead of just dumping the files in the root folder, I need to recreate the folder structure in which the original lyric files were in.
Is there any way to do this? Keep in mind, I am not very experienced when it comes to programming but I know some basics. Unfortunately I might need more of an explanation than most people here. Thank you to anyone who can help!
Here's a powershell answer:
Use Copy-Item with the -Container and -Recurse switches to copy the folder structure including files to a new location.
Copy-Item -Path "Old\Path\for\Music" -Recurse -Destination "New\Path\for\Music" -Container
If you only want to copy the lyric files while retaining the folder structure use a -Filter
Copy-Item -Path "Old\Path\for\Music" -Recurse -Filter "*.lrc" -Destination "New\Path\for\Music" -Container
You can do this in CMD
xcopy /s yourFolder\Subfolder\*.pdf destinationFolder\myfolder
powershell, recurse option keeps the folder structure
copy-item c:\\srcFolders\\* f:\\dst -force -recurse -verbose
I have a folder on file-share that I would like to migrate to SharePoint.
But there is this problem. I have given myself a full control on the folder and its sub-folders and files. Also I made myself owner of all as well.
I use Recursively count all files and folders, I get total 54,712 files
Get-ChildItem -Recurse | Measure-Object | %{$_.Count}
When I click right and properties . I get 50,750 files and 4,615 folders . So makes up 55,365 items in total.
With command prompt using dir *.* /w /s I get total 63,939
So, which one is the most reliable number of files and folders here? Why am I getting different numbers here?
Also 55,364 files has been migrated to SharePoint online.
I'm currently trying to streamline the installation process of a few products that require a powershell script to be run each, I've encountered some trouble when trying to write a script to search a directory for these files.
Example: in the directory 'Install' i have four subfolders named 'product1-4' in each of these folders there is a file 'Script.ps1'
The first issue is that the install scripts are all named the same 'script.ps1' which complicated my first idea to pull all the files from the sub-folders into a centralised location and run them all sequentially.
Feel like i'm making this more complicated than it needs to be, any advice?
Get-ChildItem $installFolder -Include *.ps1 -Recurse
That will list the .ps1 files, you could also do the following to then address the files as a single variable.
$ps1Files = Get-ChildItem $installFolder -Include *.ps1 -Recurse
I want to copy a group of folders which have various files inside them from one place to a single folder elsewhere. I want to bind said folder group to a variable.
param(
$folders=('../folder1','../folder2')
)
Copy-Item -Path $folders -Destination '../folder3' -Recurse -Force;
This works, however, inside folder3, folder1's contents are spilled out, while folder2's contents are placed in a folder of the same name just like intended.
I need them both to be copied intact, if I switch their places then folder2 gets the same treatment. It's like the script does not read the first folder in line in same way as the others. Am I missing something?
EDIT:
Managed to find a work-around by running additional command to create a folder inside "folder3" named same as first in line folder before copying. Script then places the files inside that folder correctly. Still rather messy, I wonder if it's a bug.
Use a loop
foreach($folder in $folders){
Copy-Item -Path $folder -Destination '../folder3'-Recurse -Force
}
Hope fellow scripters can help with this one :) Been breaking my head around the problem for few hours now.
I'm trying to zip up certain folders using powershell.
My folder structure is
Backups
BoxIntranet
Components
Content
Database
Exec
Files
Logs
Multibrowser
Multibrowser\Legacy\Customisation
Packages
ParentPortal
ParentPortal\customisation
StudentPortal
StudentPortal\customisation
Update
WebDav
There are a lot more files and folders in every one of the above but these are the ones I'm mainly interested in.
I am trying to zip it all up using either Write-Zip or Compress-Archive methods in PowerShell but my conditions are.
Only Content, Files, Database folders should be zipped from root
Multibrowser\Legacy\customisation, StudentPortal\Customisation and ParentPortal\customisation folders should also be backed up.
Folder structure should remain the same in the zip file meaning that Root of the zip file should have Content, Files, Database, Multibrowser, ParentPortal and StudentPortal folders. Whilst Content, Files and Database folders should have everything zipped up, Multibrowser, ParentPortal and StudentPortal folders should only have the specified sub directories and all files within them.
Code:
$FilesAndInclude = #("Content", "Files", "Database", "Multibrowser\Legacy\customisation",
"StudentPortal\customisation", "ParentPortal\customisation",
"BoxIntranet\customisation")
$FilesToExclude = #("connectionstrings.config", "inc_dbconn.asp")
Get-ChildItem -Path "C:\Folder" -Include $FilesAndInclude -Recurse -Exclude $FilesToExclude|
Compress-Archive -DestinationPath "Archive.zip"
I've tried the above and it doesn't do anything however if I remove the -Include parameter then it zips up everything however doesn't retain folder structure.
Is there any way to complete what I am after within powershell?
Ok, first things first, the reason that you are having a hard time using the -Include parameter is because it is designed to exclusively include only the things you specify. As such, it will look at the name of things (not their path), and check against the list and if it matches something in the list it will include that item. Since you only list folder names it is only including those folders (but not their contents). So you aren't getting any files passed down the pipe this way. To get around that you'll need to build your file list first, then pipe it to the cmdlet to zip things up.
Next issue is that Compress-Archive doesn't store path info, so you'll need to use Write-Zip. I have included what I think you would want for that cmdlet.
$FilesAndInclude = #("Content", "Files", "Database", "Multibrowser\Legacy\customisation",
"StudentPortal\customisation", "ParentPortal\customisation",
"BoxIntranet\customisation")
$FilesToExclude = #("connectionstrings.config", "inc_dbconn.asp")
[array]$FilesToZip = Get-ChildItem .\* -Exclude $FilesToExclude -File
$FilesToZip += $FilesAndInclude | ForEach{Get-ChildItem .\$_ -Exclude $FilesToExclude -File}
$FilesToZip | Write-Zip -EntryPathRoot $(Resolve-Path .\|Select -Expand Path) -OutputPath Archive.zip