Export non-existent file paths to text file from Powershell - powershell

Trying to locate existent OR non-existent directories via powershell and a CSV file with the supposed directories. We can get it to run and display in the console, but it will not export the findings to a txt file. It creates the file, but it is empty. Any help is useful!
Import-Csv .\missingpaths.csv | ForEach {If (Test-Path -Path $_.Path) {Write-Output"$($_.Path) exists" } Else {Write-Output "$($_.Path) does NOT exist" $_.File | Out-File .\MissingCSV.csv -Append }}

The Out-File on your code is on the else statement only (one of he reasons why formatting and indentation is important is because it can help you prevent these issues) unless this was intentional and you only wanted to export those paths that do not exist.
} Else {
Write-Output "$($_.Path) does NOT exist" $_.File |
Out-File .\MissingCSV.csv -Append
}
Give this a try:
Import-Csv .\missingpaths.csv | ForEach-Object {
[pscustomobject]#{
Path = $_.Path
TestPath = ('Does not Exist', 'Exist')[(Test-Path $_.Path)]
}
} | Export-Csv path/to/result.csv -NoTypeInformation

Related

How to check duplicate multiple file using powershell?

I want to check duplicate file.If the condition of the file like this, it means duplicate. The same name but different extension.
AAA18WWQ6BT602.PRO
AAA18WWQ6BT602.XML
I can figure out this case with my script. But I have problem if I have this more than 1 .XML file like this
AAA18WWQ6BT602.PRO
AAA18WWQ6BT602.XML
AAA18WWQ6BT601.XML
AAA18WWQ6BT604.XML
This case, it will not detect that file AAA18WWQ6BT602.PRO and AAA18WWQ6BT602.XML duplicated.
Anyone can help me please.
Thanks
$duplicate = #()
#(Get-ChildItem "$Flag_Path\*.xml") | ForEach-Object { $duplicate += $_.basename }
if(Test-Path -Path "$Flag_Path\*$duplicate*" -Exclude *.xml)
{
Get-ChildItem -Path "$Flag_Path\*$duplicate*" -Include *.xml | Out-File $Flag_Path\Flag_Duplicate
Write-Host "Flag duplicated, continue for Error_Monitoring"
pause
Error_Monitoring
}
else{
Write-Host "Flag does not duplicate, continue the process"
}
The -Include parameter only works if the path on Get-ChildItem ends in \* OR if the -Recurse switch is used.
The following should do what you want:
$flagFolder = 'D:\*'
$dupeReport = 'D:\Flag_Duplicate.txt'
$duplicates = Get-ChildItem -Path $flagFolder -File -Include '*.xml', '*.pro' |
Group-Object -Property BaseName | Where-Object { $_.Count -gt 1 }
if ($duplicates) {
# output the duplicate XML to Flag_Duplicate.txt
$duplicates.Group | Where-Object {$_.Extension -eq '.xml' } | ForEach-Object {
$_.FullName | Out-File -FilePath $dupeReport -Append
}
# do the rest of your code
Write-Host "Flag duplicated, continue for Error_Monitoring"
Error_Monitoring
}
else {
Write-Host "Flag does not duplicate, continue the process"
}
Your script does not iterate correctly. You need to have an iteration to check. The Test-Path logic looks mixed up to me. I tried to keep as much of your code as possible.
This script checks for a any xml basename filename against any suffix duplicate (not only pro):
$Flag_Path = "C:\dir_to_be_checked"
$xmlFilesArray = #()
$allFilesExceptXml = #() # all files excluding xml files
# Get all the xml files
Get-ChildItem -Path $Flag_Path -Include "*.xml" | ForEach-Object { $xmlFilesArray += $_.basename }
# Get all files from the directory the xml files
Get-ChildItem -Path $Flag_Path -Exclude "*.xml" | ForEach-Object { $allFilesExceptXml += $_.basename }
# Iterate over list of files names without suffix
ForEach ($xmlFile in $xmlFilesArray) {
ForEach ($fileToCheck in $allFilesExceptXml) {
If ($xmlFile -eq $fileToCheck) {
# logging the duplicate file (specifying utf8 or the output would be UTF-16)
Write-Output "$Flag_Path\$xmlFile.xml" | Out-File -Append -Encoding utf8 $Flag_Path\Flag_Duplicate
Write-Host "Flag duplicated, continue with duplicate search"
# pause
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Error_Monitoring
} Else {
Write-Host "Flag is not duplicated. Continue with the search."
}
}
}

PowerShell search for files from CSV and output dir if found

I've been trying to piece together a PowerShell script from what I've read on here to search a CSV list of filenames in a directory and subdirectories and then if found output the directory path with the filename out into a CSV, or if it is not found just the filename with not found next to it.
Below is what I've done so far, it works to an extent however it is not 'finding' the files, it just reports them all not found even though the files are there to be found in my test folder.
Import-Csv -Path files.csv | ForEach-Object {
Get-ChildItem -File -Recurse $_.filename
if ($_.filename -eq $_.Name) {
$_.path = $_.FullName
} else {
$_.path = "NOT FOUND"
}
$_
} | Select-Object filename,path | Export-Csv -Path output.csv -NoTypeInformation
What if the file is found in more than one folder?
storing the found folder(s), creating a [PSCustomObject] and assigning output to variable $Result
Incorporating Lee_Daileys helpful hint
## Q:\Test\2019\01\02\SO_54001484.ps1
$Result = ForEach ($file in (Import-Csv -Path files.csv)){
$FoundInFolders = (Get-ChildItem $file.filename -File -Recurse ).FullName
if ($FoundInFolders.Count -eq 0) {
$FoundInFolders = "NOT FOUND"
}
[PSCustomObject]#{
filename = $file.filename
path = $FoundInFolders -join ', '
}
}
$Result #| Export-Csv -Path output.csv -NoTypeInformation
Sample output:
> Q:\Test\2019\01\02\SO_54001484.ps1
filename path
-------- ----
test.cmd NOT FOUND
test.csv Q:\Test\2018\03\20\1\test.csv, Q:\Test\2018\03\20\2\test.csv, Q:\Test\2018\03\20\3\test.csv
test.ps1 Q:\Test\2018\03\31\test.ps1
blabla NOT FOUND

Find a string in a text file and append text at the end

I have an issue with a Firefox module. To resolve the issue I need to append text to a text file that is found under each user's firefox profile.
I can copy the new text file to each users profile using the below powershell script:
# Check to see if AWP is actually installed
$chkAWPExists = "C:\Program Files\Middleware Directory\Middleware"
$aWPExists = Test-Path $chkAWPExists
# If AWP Exists copy the pkcs11.txt file to all Firefox Profiles found.
If ($aWPExists -eq $True) {
Write-Log "AWP Exists. Copying pkcs11.txt to all firefox profiles"
Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\pkcs11.txt' |
ForEach-Object {
Copy-Item -Path 'pkcs11.txt' -Destination $_.DirectoryName
}
}
else {
Write-Log "AWP doesn't seem to be installed. Please install Oberthur Authentic web pack before activating this module."
ExitCode 1
}
But my issue is that each text file seems to have a unique info to each user. It may make sense to just append the new text lines on the end of the txt file.
Can anyone help me figure out how to append text to each users profile and loop through them all?
This is an example of two lines of text i need to add:
library=etpkcs11.dll
name=eToken PKCS#11 Module
Any help would be appreciated.
I also can't think of how im going to resolve new profiles once i run the script. But that's a battle for another day.
This should work:
$toAppend = "
library=etpkcs11.dll
name=eToken PKCS#11 Module
"
# Check to see if AWP is actually installed
$chkAWPExists = "C:\Program Files\Middleware Directory\Middleware"
$aWPExists = Test-Path $chkAWPExists
# If AWP Exists copy the pkcs11.txt file to all Firefox Profiles found.
If ($aWPExists -eq $True) {
Write-Log "AWP Exists. Copying pkcs11.txt to all firefox profiles"
ForEach ($file in (Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\pkcs11.txt')) {
$toAppend | Out-File -Append $file -Encoding 'Ascii'
}
}
else {
Write-Log "AWP doesn't seem to be installed. Please install Oberthur Authentic web pack before activating this module."
ExitCode 1
}
What i changed is the following section of code (the foreach loop) :
Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\pkcs11.txt' |
ForEach-Object {
Copy-Item -Path 'pkcs11.txt' -Destination $_.DirectoryName
}
to:
ForEach ($file in (Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\pkcs11.txt')) {
$toAppend | Out-File -Append $file -Encoding 'Ascii'
Instead of passing the Get-Childitem call to Foreach-Object, it is instead calling Get-Childitem in the foreach loop making $file the path to the specific file.
The actual append is simply passing the $toAppend variable (defined at the top) to the Out-File -Append function:
$toAppend | Out-File -Append $file

Powershell check whether a file exists

On Windows 7 x64, Powershell 3.0:
I have a directory where I download files to, this is done with a .csv file.
A sample of the files is:
008KS04.zip
008KS05.zip
008KS06.zip
008KS07.zip
008KS08.zip
008KS09.zip
I have a Csv, op wich this is a sample:
Documentnummer,Versie,Status,Geocode,IDS datum,Documentgroep (Omsch),Documenttype (Wrd),Tekeningnummer
550164,I,AB,008,26.03.2016,Themalagen OI,KS,008KS09.zip
550163,D,AB,008,19.07.2013,Themalagen OI,KS,008KS08.zip
550162,D,AB,008,19.07.2013,Themalagen OI,KS,008KS07.zip
550161,C,AB,008,19.07.2013,Themalagen OI,KS,008KS06.zip
550160,D,AB,008,19.07.2013,Themalagen OI,KS,008KS05.zip
550159,E,AB,008,15.08.2014,Themalagen OI,KS,008KS04.zip
So, lets say the files are in c:\test\
and the csv in C:\drawings.csv
I want the files that are missing in a csv, lets say C:\missing.csv
At first I tried with one drawing in a variable:
$drawnr = '008KS09.zip'
$destcsv = "C:\missing.csv"
Set-Location C:\test
If(Test-Path -Path ${drawnr}) {
Write-Output "${drawnr}'.zip' exists"
} Else {
Write-Output "${drawnr}'.zip' doesnt exist"
${drawnr} | Export-Csv $destcsv -Append -NoTypeInformation
}
The output when having an existing and a non existsing drawing in the variable
This works perfect, see image but using the CSV as input, I can't get it to work.
This is what I have so far:
$sourcecsv = 'C:\drawings.csv'
$destcsv = 'C:\missing.csv'
Set-Location 'C:\test\'
Import-Csv $sourcecsv | Select-object -Property Tekeningnummer | ForEach {
Write-Output "${_} is the current object" #For Testing purpuse
If (Test-Path -Path ${_}) {
Write-Output "${_} does exist"
} Else {
Write-Output "${_} doesnt exist"
${_} | Export-Csv $destcsv -Append -NoTypeInformation
}
}
This tells me that none of the files exist, while all do exist.
See the picture to see the output of PowerShell and the content written to the missing.csv
The picture of the output where all files are reported as non existend
I'm not sure I get your intent, but it looks like this might work.
$sourcecsv = 'C:\drawings.csv'
$destcsv = 'C:\missing.csv'
Set-Location 'C:\test\'
Import-Csv $sourcecsv | ForEach {
Write-Output "${_} is the current object" #For Testing purpuse
$TK = $_.Tekeningnummer
If (Test-Path -Path $TK) {
Write-Output "$TK does exist"
} Else {
Write-Output "$TK doesnt exist"
$_ | Export-Csv $destcsv -Append -NoTypeInformation
}
}
not sure if ${_} is a new syntax I'm not aware of but I can't get it to work on my PS4 host so I'm changing that to the normal $_ in my answer. beyond that you just need to do an ExpandProperty as #DaveSexton suggests.
Import-Csv $sourcecsv | Select-object -ExpandProperty Tekeningnummer | ForEach {
Write-Output "$($_) is the current object" #For Testing purpuse
If (Test-Path -Path $($_) {
Write-Output "$($_) does exist"
} Else {
Write-Output "$($_) doesnt exist"
$_ | Export-Csv $destcsv -Append -NoTypeInformation
}
}
just do it ;)
$sourcecsv = 'C:\drawings.csv'
$destcsv = 'C:\missing.csv'
$dirtest='C:\test\'
Import-Csv $sourcecsv | where {-not (Test-Path "$dirtest$($_.Tekeningnummer)") } | export-csv $destcsv -NoTypeInformation

pipe foreach loop CSV PowerShell

I've written a script but cannot get it to export to CSV or output in any way.
PowerShell does not like foreach loops and exporting.
For each "folderpath/filename" in the .txt file it checks to see if the file is still there and outputs true or false + folderpath/file name.
Script works fine, just cannot get the thing to export to CSV.
Any ideas what I'm doing wrong?
foreach ($WantFile in Get-Content "C:\scripts\folderpaths.txt") {
$FileExists = Test-Path $WantFile
if ($FileExists -eq $True) {
Write-Output $wantfile "True"
} else {
Write-Output $wantfile "False"
}
} | Export-Csv C:\scripts\output.csv -noType
Change your code to this:
Get-Content 'C:\scripts\folderpaths.txt' | % {
if (Test-Path -LiteralPath $_) {
Write-Output $_ "True"
} else {
Write-Output $_ "False"
}
} | Export-Csv 'C:\scripts\output.csv' -NoType
I doubt that the resulting file will contain what you expect, though. Export-Csv exports the properties of objects. The output you generate are string objects (2 with each Write-Output statement, actually), and their only property is Length, so your result will be one column with the lengths of the strings you echo.
To create a CSV with 2 columns, one for path and the other for existence of the path you need to create objects with the desired properties, e.g. like this:
Get-Content 'C:\scripts\folderpaths.txt' `
| select #{n='Path';e={$_}}, #{n='Exists';e={Test-Path -LiteralPath $_}} `
| Export-Csv 'C:\scripts\output.csv' -NoType
With regard to the original question (exporting the output of a foreach loop to CSV), you can make that output to the pipeline by wrapping it in a subexpression, but that's not going to solve the other problems in your script with regard to what it is you're trying to export:
$(ForEach ($WantFile in Get-Content "C:\scripts\folderpaths.txt"){
$FileExists = Test-Path $WantFile
If ($FileExists -eq $True) {Write-Output $wantfile "True"}
Else {Write-Output $wantfile "False"}
})| export-csv C:\scripts\output.csv -noType
I got the same problem and i got it worked by doing as follow.
$forloop = foreach ( $i in $computers)
{
$i
$out = .\psexec.exe \\$i C:\hp\hpsmh\bin\smhlogreader.exe --version
$out
}
$forloop | Out-file C:\scripts\output.txt -Append