Powershell Tips and Tricks (and commandline)

Posted on 10/13/2014 @ 12:52 AM in #SharePoint by | Feedback | 4433 views

Here are some of my favorite powershell tips and tricks (and some general commandline stuff too).

First, some pure command line stuff,

1. Find a file on your hard disk -
      dir filename /s

2. Find a file on your hard disk without all the directory info goo -
      dir filename /s/b

3. Create a list of files, say your MP3s to share with a friend? (disclaimer: don't do this piracy crap)
    dir *.mp3 /b > filelist.txt

4. Find a directory -
      dir dirname /s /ad

5. Move between directories easily
     C:\Documents and Settings\Sahil Malik>pushd .
     C:\Documents and Settings\Sahil Malik>cd \
     C:\Documents and Settings\Sahil Malik>

6. Hit Tab for directory/filename completion

7. Press F7 for a previous command list. User Cursor keys to select the command you need.

8. View all environment variables

9. View only system variables that start with W
Set W

10. View a particular env. variable

11. Find what process has opened what port
netstat –noa

12. Widen your command prompt.
mode 120,100

13. View your TCP/IP settings.
netsh interface ip show config

14. Weird looking colors
color 8f

15. Find a string in a list of files, for example "Foo" in all .cs files in your project tree:
findstr /i /s "foo" *.cs
(You might know that the windows search doggie does an awful job at search thru files, I think it just ignores files with recognized extensions, so .cs will be ignored YUK).

16. Refresh the netbios nametables:
nbtstat –R

17. Get number of open client connections on a web front end
Get-Counter -Counter 'web service(_total)\current connections' -ComputerName servername

18. Lost an application Pool password? No problem -

cmd.exe /c $env:windir\system32\inetsrv\appcmd.exe list apppool “SharePoint - 80" /text:ProcessModel.Password

And now for some Powershell stuff with some SharePoint influence

19. In SharePoint you see that Synch button? Causing you a heartache? You can kill it universally on a farm using this -

Get-SPSite -limit all | get-SPWeb -limit all | Foreach {$_.Title = $_.Title;
$_.ExcludeFromOfflineClient=1; $_.Update()}

20. PowerShell 3.0 grid stuff, you just have to try this,

$web = Get-SPWeb http://sp
$list = $web.Lists | Out-GridView -PassThru

21. Find files with matching text
ls -r | ?{$_ | Select-String -Pattern "texttosearch"}

22. Extract a WSP from SharePoint central administration as a file

$farm = Get-SPFarm

23. A correlation ID giving you heartache on a multiple server farm? Merge it all up for just a single correlation ID with this -

Merge-SPLogFile –Path c:\whateverfile.log –Correlation <correlationID>

24. ULS Viewer is too damn slow for you? Find full error messages zippity zip fast

Get-SPLogEvent | Where {$_.Correlation -Eq "f463c19c-e41f-f09e-a2ef-b8a749edf422" } | ft Message

25. Blame your sysadmins when they screw up a installation


26. Accidentally deleted a site collection? No problem, you can restore it without resorting to database backups,

Get-SPDeletedSite (gives you the ID)

Restore-SPDeletedSite (restores it)

27. SharePoint isn’t great when it comes to database consistency. I like to say SharePoint and SQL Server are an arranged marriage where SQL Server is the abused bitch. You can find orphaned objects with this,

$cdb = get-SPContentDatabase


28. So you have an object, but what methods and properties does it support? Find out like this,

$web = Get-SPWeb http://sp

$web | Get-Member

29. Get some fresh air, check the weather outside,

Invoke-RestMethod -Uri "http://api.openweathermap.org/data/2.5/weather?q=Zagreb"

30. Got something work? And you’d like to save the commands used to get there? Just do this -

Get-History | Select –Expand CommandLine | Out-File script.ps1

31. Make your computer more fun,

iex (New-Object Net.WebClient).DownloadString(“http://bit.ly/e0Mw9w”)

32. And finally, have Powershell find you a new job,

stop-computer -force | Get-ADCOmputer -Filter *


Hope you found at least a few useful new commands here! Enjoy!

Sound off but keep it civil:

Older comments..