Get folder's content in an usable variables - powershell

Is it possible to get all files names from a directory in variables ?
Consider this environment :
Dir/File.json
Dir/File7.json
Dir/File58.exe
Is it possible so that i can get File, and File7 (only the file with .json extension) in one or two variables that i'd use later in my code ?
I test dir > test.txt but : It show everything including folder or files with an other extension, and i don't know if i can then use this .txt file to get back the names individualy.
Using PowerShell

$FileNames = Get-ChildItem -Path 'C:\Dir' -Name *.json
This will return all json files in the folder C:\Dir

Related

How to copy a list of folder names in a folder to text file in Powershell?

I have a folder, folder A in a file path, inside folder A are multiple folders (folder 1, folder 2, folder 3). I want to create a Powershell script that goes to the file path where folder A is, read the names of the folders in it and create a .txt file
Inside the .txt file it would have:
folder 1
folder 2
folder 3
I'm not sure how to do this
(there shouldn't be the extra lines in between each folder name in the .txt file, I just can't figure out how to format it properly
So I figured out the solution to my problem. What I was looking for was:
c:\Windows> Get-ChildItem -Path C:\Users... -Name | Out-File -FilePath C:\Users...\test.txt
This outputs the names of the directories that I put in the path and takes them into the test.txt file

Comparing File Names with different extensions using Powershell

I want to compare files with same name but with different extensions present in the same directory.
Example: There are 6 files at a location.
1234.pdf
1234.xml
abcd.pdf
abcd.xml
5678.pdf
efgh.xml
I want to compare all the .pdf/.xml files with the files with .xml/.pdf extension which have a same name and find out if any of the .pdf or .xml file is missing like here in the above example the 5678.xml file is missing and efgh.pdf file is missing.
I tried to copy all the .pdf files in a text file and all the .xml files in another text file and tried comparing the strings within them but it isn't working.
Can anyone please let me know how can i compare the file names with different extensions?
Push-Location "\\Cifintgfsfs001\gfs\MXPDFXML\Data\Test"
Get-childitem *.xml | ForEach {
if (!(test-path "$($_.BaseName).pdf")){
"$($_.BaseName).pdf missing"
}
}
Get-childitem *.pdf | ForEach {
if (!(test-path "$($_.BaseName).xml")){
"$($_.BaseName).xml missing"
}
}
Pop-Location

Copying a registry key

I am trying to make a simple PowerShell script that copies some registry folders and substrings in these folders to it's own folder on the C:\ drive.
How can I do this? I've tried export command, and Copy-Item -Path command but without luck.
For example copy the files (registry files) in the keys
HKLM\software\Norton
HKLM\software\Wow6432Node\Norton
to the folder C:\backupNorton.
You can do
Get-ChildItem HKLM:\SOFTWARE\Norton
And then export or save the object you get from that - either as plain text or using something like Export-Clixml.

PowerShell zip script adding many folders

how to add many folders from differents places to one file zip in PowerShell script using [io.compression.zipfile]
One way to do this is to use the Extensions
https://msdn.microsoft.com/en-us/library/system.io.compression.zipfileextensions(v=vs.110).aspx
A simple example for adding a single file would be as follows
Add-Type -Path "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll"
$NewFilePath = "c:\pat\to\file.txt"
$EntryName = "file.txt"
$zip = [io.compression.zipfile]::Open("test.zip",[io.compression.ziparchivemode]::Create)
[io.compression.ZipFileExtensions]::CreateEntryFromFile($zip, $NewFilePath, $EntryName)
$zip.Dispose()
You can just iterate over your list of files adding them to your new zip file.
Note that you can reference the type without the full path if you wish.

Report folders which have a specific subfolder

I am building a script which I use to deploy files to multiple specific folders.
The destination folders are collected using this part.
$destinations = Get-ChildItem "C:\this\is\*\my\path\"
So my script replaces only if the folder has the subfolders "\my\path\"
If I now check my variable it will return the fullpathes but I only need the folder name. I tried using select -path to show at least only the path but it returned as well the length, mode etc.
my goal is to return only values like this:
folder 1
folder 2
folder 3
I am using powershell 3.0
So if we are checking for folders that have the child structure folder1\folder2 where the parent folder is in C:\Temp then we would do something like this:
$destinations = (Get-Item "C:\Temp\*\folder1\folder2").Parent.Parent.Name
Get-Item "C:\Temp\*\folder1\folder2" would just return System.IO.DirectoryInfo objects for folder2. We take those objects and find their grandparent folders and just return their names only.