snipt

Ctrl+h for KB shortcuts

PowerShell

Retrieve filename of files containing a specific string

Get-ChildItem -Recurse -Include *.* | Select-String "PostRequest.ps1" | Select-Object -Unique Path
https://snipt.net/embed/89e2861e32978859bf599e5fe77dc63b/
https://snipt.net/raw/89e2861e32978859bf599e5fe77dc63b/
89e2861e32978859bf599e5fe77dc63b
powershell
PowerShell
1
2016-09-27T21:50:30
True
False
False
Sep 14, 2015 at 07:26 PM
/api/public/snipt/141033/
retrieve-filename-of-files-containing-a-specific-string
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nb">Get-ChildItem</span> <span class="n">-Recurse</span> <span class="n">-Include</span> <span class="p">*.*</span> <span class="p">|</span> <span class="nb">Select-String</span> <span class="s2">&quot;PostRequest.ps1&quot;</span> <span class="p">|</span> <span class="nb">Select-Object</span> <span class="n">-Unique</span> <span class="n">Path</span> </span></pre></div> </td></tr></table>
powershell

PowerShell

Find file by filename (excluding extension)

Get-ChildItem -Path "C:\Mercurial\client_3.0" -Recurse | Where-Object { !$PsIsContainer -and [System.IO.Path]::GetFileNameWithoutExtension($_.Name) -eq "Log" }
https://snipt.net/embed/ab6b331f1159936f124728196dd42694/
https://snipt.net/raw/ab6b331f1159936f124728196dd42694/
ab6b331f1159936f124728196dd42694
powershell
PowerShell
1
2016-09-30T13:01:39
True
False
False
/api/public/snipt/46995/
find-file-by-filename-excluding-extension
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nb">Get-ChildItem</span> <span class="n">-Path</span> <span class="s2">&quot;C:\Mercurial\client_3.0&quot;</span> <span class="n">-Recurse</span> <span class="p">|</span> <span class="nb">Where-Object</span> <span class="p">{</span> <span class="p">!</span><span class="nv">$PsIsContainer</span> <span class="o">-and</span> <span class="no">[System.IO.Path]</span><span class="p">::</span><span class="n">GetFileNameWithoutExtension</span><span class="p">(</span><span class="nv">$_</span><span class="p">.</span><span class="n">Name</span><span class="p">)</span> <span class="o">-eq</span> <span class="s2">&quot;Log&quot;</span> <span class="p">}</span> </span></pre></div> </td></tr></table>
powershell

PowerShell

Find in files

Get-ChildItem -Recurse -Include *.* | Select-String "text to search for"
https://snipt.net/embed/290f382b23225f2f7252ba219e11e2b8/
https://snipt.net/raw/290f382b23225f2f7252ba219e11e2b8/
290f382b23225f2f7252ba219e11e2b8
powershell
PowerShell
1
2016-09-24T11:49:50
True
False
False
Jan 16, 2013 at 06:39 PM
/api/public/snipt/46994/
find-in-files
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nb">Get-ChildItem</span> <span class="n">-Recurse</span> <span class="n">-Include</span> <span class="p">*.*</span> <span class="p">|</span> <span class="nb">Select-String</span> <span class="s2">&quot;text to search for&quot;</span> </span></pre></div> </td></tr></table>
powershell

PowerShell

Powershell - Change timestamp on files

gci -rec | where-object {$_.lastWriteTime -gt (get-date "2012-07-13T00:00:00")} | %{$_.lastWriteTime = ($_.lastAccessTime = ($_.creationTime = (get-date "2012-07-12T00:00:00")))}
https://snipt.net/embed/ab3468005aef13d40f8ee4bd9d820b0e/
https://snipt.net/raw/ab3468005aef13d40f8ee4bd9d820b0e/
ab3468005aef13d40f8ee4bd9d820b0e
powershell
PowerShell
1
2016-09-30T09:02:45
True
False
False
Apr 03, 2013 at 07:52 PM
/api/public/snipt/43940/
powershell-retrieve-files-that-were-last-modified-after-a-certain-timestamp-then-change-their-timestamp
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="n">gci</span> <span class="n">-rec</span> <span class="p">|</span> <span class="nb">where-object</span> <span class="p">{</span><span class="nv">$_</span><span class="p">.</span><span class="n">lastWriteTime</span> <span class="o">-gt</span> <span class="p">(</span><span class="nb">get-date</span> <span class="s2">&quot;2012-07-13T00:00:00&quot;</span><span class="p">)}</span> <span class="p">|</span> <span class="p">%{</span><span class="nv">$_</span><span class="p">.</span><span class="n">lastWriteTime</span> <span class="p">=</span> <span class="p">(</span><span class="nv">$_</span><span class="p">.</span><span class="n">lastAccessTime</span> <span class="p">=</span> <span class="p">(</span><span class="nv">$_</span><span class="p">.</span><span class="n">creationTime</span> <span class="p">=</span> <span class="p">(</span><span class="nb">get-date</span> <span class="s2">&quot;2012-07-12T00:00:00&quot;</span><span class="p">)))}</span> </span></pre></div> </td></tr></table>
powershell

PowerShell

Merge all files into one

1
2
3
4
5
# Note that this will merge all files in the current directory, in powershell terminal navigate to directory that contains files

get-childitem | foreach {
    add-content -value (get-content $_) -path c:\SomeMergedFile.txt
}
https://snipt.net/embed/fe7c64db56d250843aebe716c1efd2db/
https://snipt.net/raw/fe7c64db56d250843aebe716c1efd2db/
fe7c64db56d250843aebe716c1efd2db
powershell
PowerShell
5
2016-09-30T16:05:59
True
False
False
/api/public/snipt/43339/
powershell-merge-all-files-into-one
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="c"># Note that this will merge all files in the current directory, in powershell terminal navigate to directory that contains files</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="nb">get-childitem</span> <span class="p">|</span> <span class="k">foreach</span> <span class="p">{</span> </span><span id="L-4"><a name="L-4"></a> <span class="nb">add-content</span> <span class="n">-value</span> <span class="p">(</span><span class="nb">get-content</span> <span class="nv">$_</span><span class="p">)</span> <span class="n">-path</span> <span class="n">c</span><span class="err">:</span><span class="p">\</span><span class="n">SomeMergedFile</span><span class="p">.</span><span class="n">txt</span> </span><span id="L-5"><a name="L-5"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
powershell, windows

PowerShell

Bulk filename replacement

1
2
3
# In terminal navigate to directory where rename is to be performed and run:

Dir | Rename-Item -NewName { $_.name -replace "ToReplace","ReplaceWithThis"}
https://snipt.net/embed/4c0d34f0ea7083b6e55a95177d7e2e1d/
https://snipt.net/raw/4c0d34f0ea7083b6e55a95177d7e2e1d/
4c0d34f0ea7083b6e55a95177d7e2e1d
powershell
PowerShell
3
2016-09-28T05:54:04
True
False
False
/api/public/snipt/43338/
powershell-bulk-filename-replacement
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="c"># In terminal navigate to directory where rename is to be performed and run:</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="n">Dir</span> <span class="p">|</span> <span class="nb">Rename-Item</span> <span class="n">-NewName</span> <span class="p">{</span> <span class="nv">$_</span><span class="p">.</span><span class="n">name</span> <span class="o">-replace</span> <span class="s2">&quot;ToReplace&quot;</span><span class="p">,</span><span class="s2">&quot;ReplaceWithThis&quot;</span><span class="p">}</span> </span></pre></div> </td></tr></table>
powershell, windows

Text only

Using Reflection to load Microsoft.Web.Administration in PowerShell

1
2
3
4
[System.Reflection.Assembly]::LoadFrom("C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll")
$sm = new-object Microsoft.Web.Administration.ServerManager
$sm.Sites["test2"].Delete()
$sm.CommitChanges()
https://snipt.net/embed/9566fd81d55ab8acabc6f22f3728c31e/
https://snipt.net/raw/9566fd81d55ab8acabc6f22f3728c31e/
9566fd81d55ab8acabc6f22f3728c31e
text
Text only
4
2016-09-29T15:28:08
True
False
False
/api/public/snipt/37601/
using-reflection-to-load-microsoftwebadministration-in-powershell
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a>[System.Reflection.Assembly]::LoadFrom(&quot;C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll&quot;) </span><span id="L-2"><a name="L-2"></a>$sm = new-object Microsoft.Web.Administration.ServerManager </span><span id="L-3"><a name="L-3"></a>$sm.Sites[&quot;test2&quot;].Delete() </span><span id="L-4"><a name="L-4"></a>$sm.CommitChanges() </span></pre></div> </td></tr></table>
powershell, reflection
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.