excel/powershell - find multiple cells that match a string not just the last one - excel

Is it possible to find all cell references that contain a specific string in powershell? $Range.Find always just returns the last cell in the sheet, I want all the cells (or do I have to loop through each row? groan).
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$Workbook = $Excel.Workbooks.Open("C:\Users\some.xlsx")
$MySheet = $Excel.Worksheets.Item("somesheet")
$Range = $MySheet.UsedRange
$Targets = $Range.Find("Cheese")
Foreach ($Target in $Targets)
{
Write-Host $Target.AddressLocal()
}

I think you should use:
$Target = $Range.Find("Cheese")
$First = $Target
Do
{
Write-Host $Target.AddressLocal()
$Target = $Range.FindNext($Target)
}
While ($Target -ne $NULL -and $Target.AddressLocal() -ne $First.AddressLocal())

Related

Powershell Excel Code changing table info

Hello all I need a little help with my code not sure what I am doing wrong or why its not working. Slowly learning PS to automate some of my work.
I have this code, What I am trying to do it have it clear all filters of all the excel files in a folder. Then have it search some of the columns in a table and replace all the blanks with N\A.
So far I have it working to clear all filters and it replaces blanks with N/A but it does not stop in the table it just continues.
$Path = "C:\Users\Username\Downloads\Workstations (21)\Workstations\"
$files = Get-ChildItem "C:\Users\Username\Downloads\Workstations (21)\Workstations" -Filter *.xlsx
ForEach ($item in $files) {
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $true
$Workbook = $Excel.workbooks.open($Path + [System.IO.Path]::GetFileName("$item"))
$Worksheets = $Workbooks.worksheets
$Worksheet = $Workbook.Worksheets.Item(2)
$worksheet.AutoFilter.ShowAllData()
$SearchString = "" #String to Find
$Range = $Worksheet.ListObjects("Table1").ListColumns("Column3").Range # Column or Columns want to search single "R4" multi "R4:Y4"
$Search = $Range.find($SearchString) }
$Search = $Range.find($SearchString)
if ($search -ne $null) {
$FirstAddress = $search.Address
do {
$Search.value() = "N/A" # Replacement Value
$search = $Range.FindNext($search)
} while ( $search -ne $null -and $search.Address -ne $FirstAddress)
}
$WorkBook.Save()
$WorkBook.Close()
[void]$excel.quit()

Replace a string of text in multiple Excel files using a PowerShell script

I have multiple Excel files that have similar data, and I want a PowerShell script that searches and replaces a string of text in all Excel files in a specified folder.
So far, I have the following PowerShell script that opens all my Excel files and attempts to replace matching strings in the second Worksheet, but the expected changes are not made:
$Path = "C:\Users\mabrant\Downloads\Workstations (21)\Workstations\"
$files = Get-ChildItem "C:\Users\mabrant\Downloads\Workstations (21)\Workstations" -Filter *.xlsx
ForEach ($item in $files) {
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $true
$Workbook = $Excel.workbooks.open($Path + [System.IO.Path]::GetFileName("$item"))
$Worksheets = $Workbooks.worksheets
$Worksheet = $Workbook.Worksheets.Item(2)
$SearchString = "NEW" #String to Find
$Range = $Worksheet.Range("S4:Y4").EntireColumn #Range of Cells to look at
$Search = $Range.find($SearchString) }
$Search = $Range.find($SearchString)
if ($search -ne $null) {
$FirstAddress = $search.Address
do {
$Search.value() = "Installed" # Replacement Value
$search = $Range.FindNext($search)
} while ( $search -ne $null -and $search.Address -ne $FirstAddress)
}
$WorkBook.Save()
$WorkBook.Close()
[void]$excel.quit() `
Seems like you were really close to having a working script. I believe the main problem is that your ForEach block should include everything except the $excel.quit() so that you save and close each workbook as you go.
I reformatted your code to make it easier to see the entire ForEach block, I removed the duplicate $Search = $Range.find($SearchString) statement, and I set some Excel.Application properties to $false to make it work better.
Here is the updated code:
$Path = "C:\Users\mabrant\Downloads\Workstations (21)\Workstations\"
$files = Get-ChildItem $Path -Filter *.xlsx
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
$Excel.EnableEvents = $false
$Excel.DisplayAlerts = $false
ForEach ($item in $files) {
$Workbook = $Excel.Workbooks.Open($Path + [System.IO.Path]::GetFileName("$item"))
$Worksheet = $Workbook.Worksheets.Item(2)
$SearchString = "NEW" #String to Find
$Range = $Worksheet.Range("S4:Y4").EntireColumn #Range of Cells to look at
$Search = $Range.find($SearchString)
if ($Search -ne $null) {
$FirstAddress = $Search.Address
do {
$Search.Value() = "Installed" # Replacement Value
$Search = $Range.FindNext($Search)
} while ( $Search -ne $null -and $Search.Address -ne $FirstAddress)
}
$WorkBook.Save()
$WorkBook.Close()
}
$Excel.Quit()

Find a value in Excel via PowerShell

I have a script that find a row with a specific background color. I want to add a condition that if the cell has a color 14 and contains the word cab and I will copy it to a different folder. All the greens (color 14)will copy to other folder. currently all the green cells (14) copied to the same folder
Maybe I need more if condition ? or one more object that holds all the cell that has color 14 and with the string inside? (patch is the name of the column)
I need an object with all the 14 colors and one object with all the 14 colors and has a name like cab
$ExcelFile = "C:\Temp\SharedFolder\Side VIP - Bulk Tool.xlsx"
$searchValue = ''
$excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
$Excel.DisplayAlerts = $False # Disable comfirmation prompts
$workbook = $excel.Workbooks.Open($ExcelFile)
$worksheet = $workbook.Worksheets.Item("VIP List")
# get the number of rows in the sheet
$rowMax = $worksheet.UsedRange.Rows.Count
# loop through the rows to test if the value in column 1 equals whatever is in $searchValue
# and capture the results in variable $result
$result = for ($row = 1; $row -le $rowMax; $row++) {
$val = $worksheet.Cells.Item($row, 27).Interior.ColorIndex
if ($val -eq 14 -and $val -ne "cab") {
[PsCustomObject]#{Patch = $worksheet.Cells.Item($row, 1).Value2}
}
}
write-host
-join("Number of patches:" + $result.count)
write-host
#$val = $worksheet.Cells.Item($row, 1).Interior.ColorIndex; if ($val -eq 3) { ... }
foreach ($res in $result)
{$vars = foreach ($res in $result) { "\\google.com\global\Patch Managment\$($res.patch)\*" }}
$des = "C:\Temp\SharedFolder\SideVIP"
foreach ($var in $vars)
{
write-host $var
Copy-Item -Path $var -include "*.VIP","*.ZIP"-Destination $des -Force
}

Find and Replace in Excel with PowerShell

I have a column with computer names however the names are formatted like this domain\computer name. Usually in Excel I can use find/replace and search for *\ and replace with blank leaving just the computer name. I have a powershell script that finds *\ however it replaces the whole cell with nothing. It's like its searching for any cell that contains *\ and replacing that instead of just leaving the computer name.
$file = "C:\reports\report.xls"
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$Workbok = $Excel.workbooks.open($file)
$Worksheets = $Workbook.worksheets
$Worksheet = $Workbook.Worksheets.Item(1)
$Worksheet = $Workbok.Worksheets.Item(1)
$SearchString = "*\"
$Range = $Worksheet.Range("A1").EntireColumn
$Search = $Range.find($SearchString)
if($search -ne $null){
$FirstAddress = $Search.Address
do{
$Search.value() = ""
$search = $Range.FindNext($search)
}while ($search -ne $null -and $search.Address -ne $FirstAddress)
In your Do-While loop you are taking the value of the search and setting it to an empty string. $Search.value() = "". If you take that and change it something like this $search.Value() = $Search.value() -replace "^.*\\",'' it should work. I used -replace with some regex to replace everything from the backslash until the beginning of the line.
Cleaned up your code a bit because it seems you had some misspelled variables
$file = "C:\reports\report.xls"
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$Workbook = $Excel.workbooks.open($file)
$Worksheet = $Workbook.Worksheets.Item(1)
$SearchString = "*\"
$Range = $Worksheet.Range("A1").EntireColumn
$Search = $Range.find($SearchString)
if ($search -ne $null) {
$FirstAddress = $Search.Address
do {
$search.Value() = $Search.value() -Replace "^.*\\", ''
$search = $Range.FindNext($search)
} while ($search -ne $null -and $search.Address -ne $FirstAddress)
}

How to search contain text in column and return data in new column using PowerShell?

I have an Excel file with many columns, and I need to search and get data that contains a search text and return it to a new column. My data as below.
Column A:
xxxxx\detail\A1\yyyy
aaaaa\detail\A2\zzzz
A3
I want the following result.
Column B:
detail\A1
detail\A2
A3
My code is below, but it returns the position of the search text instead.
$File = "C:\\Testt.xlsx"
$SearchString = "detail\" + "*"+ "\"
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open($File)
for ($i = 1; $i -lt $($Workbook.Sheets.Count() + 1); $i++) {
$Range = $Workbook.Sheets.Item($i).Range("A:A")
$Target = $Range.Find($SearchString)
$First = $Target
do {
Write-Host "$i $($Target.AddressLocal())"
$Target = $Range.FindNext($Target)
} while ($Target -ne $null -and $Target.AddressLocal() -ne $First.AddressLocal())
}
$Excel.Quit()

Resources