{"id":1011,"date":"2025-04-17T13:11:23","date_gmt":"2025-04-17T13:11:23","guid":{"rendered":"https:\/\/techno.slomka.biz\/?p=1011"},"modified":"2026-03-10T17:00:14","modified_gmt":"2026-03-10T17:00:14","slug":"powershell-search-directories-and-subdirectories-for-files-with-specific-content","status":"publish","type":"post","link":"https:\/\/techno.slomka.biz\/?p=1011","title":{"rendered":"\ud83d\udca1Powershell Tip &#8211; Search directories and subdirectories for files with specific content"},"content":{"rendered":"\n<p>I&#8217;d like to share a small PowerShell script that scans all subdirectories for specific files (for example, *.yaml and *.yml) and searches them for a specific string (e.g., &#8220;ansible.builtin.template&#8221;). The result is returned with the location (name and path of the file, and line number of the search string in the file).<\/p>\n\n\n\n<p>What it does:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Recursively searches all .yaml and .yml files starting from the current directory (.).<\/li>\n\n\n\n<li>Searches for the string service time in each file.<\/li>\n\n\n\n<li>Prints the file, line number, and line contents for each match.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PowerShell<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># String to search for\n$searchterm= \"ansible.builtin.template\"\n\n# Search all .yaml and .yml files recursively\nGet-ChildItem -Path . -Recurse -Include *.yaml, *.yml -File | ForEach-Object {\n    $Datei = $_.FullName\n    # Search the file contents line by line\n    Select-String -Path $Datei -Pattern $searchterm| ForEach-Object {\n        &#91;PSCustomObject&#93;@{\n            Datei     = $_.Path\n            Zeile     = $_.LineNumber\n            Inhalt    = $_.Line.Trim()\n        }\n    }\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\"># String to search for<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">$searchterm<\/span><span style=\"color: #D4D4D4\">= <\/span><span style=\"color: #CE9178\">&quot;ansible.builtin.template&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Search all .yaml and .yml files recursively<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">Get-ChildItem<\/span><span style=\"color: #D4D4D4\"> -Path . -Recurse -Include *.yaml, *.yml -File | <\/span><span style=\"color: #DCDCAA\">ForEach-Object<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$Datei<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">$_<\/span><span style=\"color: #DCDCAA\">.FullName<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Search the file contents line by line<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">Select-String<\/span><span style=\"color: #D4D4D4\"> -Path <\/span><span style=\"color: #9CDCFE\">$Datei<\/span><span style=\"color: #D4D4D4\"> -Pattern <\/span><span style=\"color: #9CDCFE\">$searchterm<\/span><span style=\"color: #D4D4D4\">| <\/span><span style=\"color: #DCDCAA\">ForEach-Object<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        &#91;<\/span><span style=\"color: #569CD6\">PSCustomObject<\/span><span style=\"color: #D4D4D4\">&#93;<\/span><span style=\"color: #569CD6\">@<\/span><span style=\"color: #D4D4D4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #9CDCFE\">Datei<\/span><span style=\"color: #D4D4D4\">     = <\/span><span style=\"color: #9CDCFE\">$_<\/span><span style=\"color: #DCDCAA\">.Path<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #9CDCFE\">Zeile<\/span><span style=\"color: #D4D4D4\">     = <\/span><span style=\"color: #9CDCFE\">$_<\/span><span style=\"color: #DCDCAA\">.LineNumber<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #9CDCFE\">Inhalt<\/span><span style=\"color: #D4D4D4\">    = <\/span><span style=\"color: #9CDCFE\">$_<\/span><span style=\"color: #DCDCAA\">.Line.Trim<\/span><span style=\"color: #D4D4D4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Sometimes I want to save my results to a file and explore them further. To save the results to a CSV file, adapt the script as follows:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PowerShell<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>\n$results= Get-ChildItem -Path . -Recurse -Include *.yaml, *.yml -File | ForEach-Object {\n    $Datei = $_.FullName\n    Select-String -Path $Datei -Pattern $Suchbegriff | ForEach-Object {\n        &#91;PSCustomObject&#93;@{\n            Datei     = $_.Path\n            Zeile     = $_.LineNumber\n            Inhalt    = $_.Line.Trim()\n        }\n    }\n}\n\n$results | Export-Csv -Path \"searchresults.csv\" -NoTypeInformation -Encoding UTF8\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">$results<\/span><span style=\"color: #D4D4D4\">= <\/span><span style=\"color: #DCDCAA\">Get-ChildItem<\/span><span style=\"color: #D4D4D4\"> -Path . -Recurse -Include *.yaml, *.yml -File | <\/span><span style=\"color: #DCDCAA\">ForEach-Object<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$Datei<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">$_<\/span><span style=\"color: #DCDCAA\">.FullName<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">Select-String<\/span><span style=\"color: #D4D4D4\"> -Path <\/span><span style=\"color: #9CDCFE\">$Datei<\/span><span style=\"color: #D4D4D4\"> -Pattern <\/span><span style=\"color: #9CDCFE\">$Suchbegriff<\/span><span style=\"color: #D4D4D4\"> | <\/span><span style=\"color: #DCDCAA\">ForEach-Object<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        &#91;<\/span><span style=\"color: #569CD6\">PSCustomObject<\/span><span style=\"color: #D4D4D4\">&#93;<\/span><span style=\"color: #569CD6\">@<\/span><span style=\"color: #D4D4D4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #9CDCFE\">Datei<\/span><span style=\"color: #D4D4D4\">     = <\/span><span style=\"color: #9CDCFE\">$_<\/span><span style=\"color: #DCDCAA\">.Path<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #9CDCFE\">Zeile<\/span><span style=\"color: #D4D4D4\">     = <\/span><span style=\"color: #9CDCFE\">$_<\/span><span style=\"color: #DCDCAA\">.LineNumber<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #9CDCFE\">Inhalt<\/span><span style=\"color: #D4D4D4\">    = <\/span><span style=\"color: #9CDCFE\">$_<\/span><span style=\"color: #DCDCAA\">.Line.Trim<\/span><span style=\"color: #D4D4D4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">$results<\/span><span style=\"color: #D4D4D4\"> | <\/span><span style=\"color: #DCDCAA\">Export-Csv<\/span><span style=\"color: #D4D4D4\"> -Path <\/span><span style=\"color: #CE9178\">&quot;searchresults.csv&quot;<\/span><span style=\"color: #D4D4D4\"> -NoTypeInformation -Encoding UTF8<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;d like to share a small PowerShell script that scans all subdirectories for specific files (for example, *.yaml and *.yml) and searches them for a specific string (e.g., &#8220;ansible.builtin.template&#8221;). The result is returned with the location (name and path of the file, and line number of the search string in the file). What it does: Sometimes I want to save my results to a file and explore them further. To save the results to a CSV file, adapt the script as follows:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,18,1],"tags":[78],"class_list":["post-1011","post","type-post","status-publish","format-standard","hentry","category-powershell","category-shell-scripting","category-uncategorized","tag-powershell"],"_links":{"self":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/1011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1011"}],"version-history":[{"count":8,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/1011\/revisions"}],"predecessor-version":[{"id":1088,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/1011\/revisions\/1088"}],"wp:attachment":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}