{"id":2021,"date":"2022-02-11T12:52:40","date_gmt":"2022-02-11T12:52:40","guid":{"rendered":"https:\/\/stevenwhiting.com\/blog\/?p=2021"},"modified":"2022-03-07T13:20:47","modified_gmt":"2022-03-07T13:20:47","slug":"powershell-replace-characters","status":"publish","type":"post","link":"https:\/\/stevenwhiting.com\/blog\/?p=2021","title":{"rendered":"Powershell &#8211; replace characters"},"content":{"rendered":"\n<p>Another one and I ended up sticking a load of notes on this for myself<\/p>\n\n\n\n<p>Get-Item &#8220;\\\\SERVERNAME\\files\\All &#8211; all general shared files\\April 2020 &#8211; Rents and service charges\\Archive*.pdf&#8221; | ForEach-Object {<br>Rename-Item $_ ($_.Name -replace &#8220;-&#8220;, &#8221; &#8220;) -WhatIf }<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>So here are my notes on the command.<\/p>\n\n\n\n<p><strong>Get-Item : We&#8217;re getting the contents of the folder on SERVERNAME and looking at all files ending in .pdf<\/strong><\/p>\n\n\n\n<p><strong>We have to put the path in &#8220;&#8221; because the folder name has spaces.<\/strong><\/p>\n\n\n\n<p><strong>We then pipe this with the pipe command (cause it looks like a pipe) | to the ForEach-Object loop. The pipe command means you pass the results of what was just to the left of it, to the next command on its right.<\/strong><\/p>\n\n\n\n<p><strong>ForEach-Object takes each file, from the Get-Item command and with Rename-Item stores each file&#8217;s name in the Powershell global variable $_. You use that global variable because its easy, its short (typing wise) and you don&#8217;t have to declare it at the top of your script like other variables. So less code. The $_. global variable is built into Powershell.<\/strong><\/p>\n\n\n\n<p><strong>The $_.Name takes the contents stored in Global Variable $_ and adds it to name with the (in this case) &#8211; removed from all the files that had it and replaced with a space. That is what &#8220;-&#8221; &#8221; &#8221; are. You&#8217;re looking for &#8220;-&#8221; in the file name and replacing with &#8221; &#8221; space.<\/strong><\/p>\n\n\n\n<p><strong>The whatif is only there so the command doesn&#8217;t actually change anything, it just shows what would happen if the command ran, it can be removed once you know the script works.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another one and I ended up sticking a load of notes on this for myself Get-Item &#8220;\\\\SERVERNAME\\files\\All &#8211; all general shared files\\April 2020 &#8211; Rents and service charges\\Archive*.pdf&#8221; | ForEach-Object {Rename-Item $_ ($_.Name -replace &#8220;-&#8220;, &#8221; &#8220;) -WhatIf } So &hellip; <a href=\"https:\/\/stevenwhiting.com\/blog\/?p=2021\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[333],"class_list":["post-2021","post","type-post","status-publish","format-standard","hentry","category-it-notes","tag-powershell"],"_links":{"self":[{"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2021"}],"version-history":[{"count":4,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2021\/revisions"}],"predecessor-version":[{"id":2028,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2021\/revisions\/2028"}],"wp:attachment":[{"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}