<commit_before>---
layout: single
title: Custom Windows File and Pattern Search in PowerShell script
excerpt: ""

tags: [PowerShell]

---
{{ page.date | date: '%B %d, %Y' }}

## Custom Windows Search

I recently worked on a project where I was tasked to create a custom search for a client and he wanted a specific search that would
look at 1 text file filled with windows path locations and 1 text file that has specific words to search for.

Here is the script:
{% gist 1593a0f632fa36765a100f085ea81015 %}

## Breakdown of script

The first function I found on an answer this question on [superuser.com](http://superuser.com/questions/468782/show-human-readable-file-sizes-in-the-default-powershell-ls-command)
This is used for formatting the size of files found in the script.

Format-FileSize function takes one integer that is the Length property of a file and formats it according to its size. i.e. 1KB, 1MB...
{% highlight PowerShell %}
    Function Format-FileSize() {
        Param ([int]$size)
        If     ($size -gt 1TB) {[string]::Format("{0:0.00} TB", $size / 1TB)}
        ElseIf ($size -gt 1GB) {[string]::Format("{0:0.00} GB", $size / 1GB)}
        ElseIf ($size -gt 1MB) {[string]::Format("{0:0.00} MB", $size / 1MB)}
        ElseIf ($size -gt 1KB) {[string]::Format("{0:0.00} kB", $size / 1KB)}
        ElseIf ($size -gt 0)   {[string]::Format("{0:0.00} B", $size)}
        ElseIf ($size -eq 0)   {"0 MB"}<commit_msg>Added header image to Windows search post.<commit_after>---
layout: single
title: Custom Windows File and Pattern Search in PowerShell script
excerpt: ""
header: 
        image: /assets/images/poshBlue.jpg
        teaser: /assets/images/poshBlue.jpg

tags: [PowerShell]

---
{{ page.date | date: '%B %d, %Y' }}

## Custom Windows Search

I recently worked on a project where I was tasked to create a custom search for a client and he wanted a specific search that would
look at 1 text file filled with windows path locations and 1 text file that has specific words to search for.

Here is the script:
{% gist 1593a0f632fa36765a100f085ea81015 %}

## Breakdown of script

The first function I found on an answer this question on [superuser.com](http://superuser.com/questions/468782/show-human-readable-file-sizes-in-the-default-powershell-ls-command)
This is used for formatting the size of files found in the script.

Format-FileSize function takes one integer that is the Length property of a file and formats it according to its size. i.e. 1KB, 1MB...
{% highlight PowerShell %}
    Function Format-FileSize() {
        Param ([int]$size)
        If     ($size -gt 1TB) {[string]::Format("{0:0.00} TB", $size / 1TB)}
        ElseIf ($size -gt 1GB) {[string]::Format("{0:0.00} GB", $size / 1GB)}
        ElseIf ($size -gt 1MB) {[string]::Format("{0:0.00} MB", $size / 1MB)}
        ElseIf ($size -gt 1KB) {[string]::Format("{0:0.00} kB", $size / 1KB)}
        ElseIf ($size -gt 0)   {[string]::Format("{0:0.00} B", $size)}
        ElseIf ($size -eq 0)   {"0 MB"}
{'fraction_non_alphanumeric': 0.13462179273575475, 'fraction_numerical': 0.0433188937020993, 'mean_word_length': 3.440828402366864, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2494022', 'n_tokens_mistral': 1076, 'n_tokens_neox': 952, 'n_words': 368}