·
Always run Powershell as Administrator
·
Make sure Add-PSSnapin "Microsoft.SharePoint.Powershell"
is added
If this reference is not added you
will get not recognized as cmdlet,function…. Error
This can be resolved by adding Add-PSSnapin
"Microsoft.SharePoint.Powershell"
·
Commenting,writing and in .ps1 files
Example : I have a Powershell.ps1
--Start of Script---
#Test.ps1 This is will add comment
to Test powershell script
#Add
Add-PSSnapin
"Microsoft.SharePoint.Powershell"
#This will write to output window
Write-Host This is Test powershell
script
#Writing in different colours
Write-Host -f Red I am Red
Write-Host -f Yellow I am Red
#This will make sure the output does
not disappear
powershell –noexit
--End of Script--
You can copy paste the script in txt
file and save it as Powershell.ps1
On running this the output should
look like this
Note : Many times the output window
disappears and we fail to read the output, add the following command to retain
the output window
powershell –noexit
·
Calling a function
The difference
in .Net or other techniques of calling function is that in Powershell you first
need to define the function and then you can make a call to function
Also it does not
need circular bracket to call the function just the name of function(See
example below)
--Start of Script---
#Test.ps1 This
is will add comment to Test powershell script
#Add
Add-PSSnapin
"Microsoft.SharePoint.Powershell"
function
MySolution()
{
Write-Host
-f Yellow "I am in MySolution function"
#Do
something
}
#This is how you
call a function
MySolution
#This will make
sure the output does not disappear
powershell
–noexit
--End of Script--
You can copy paste the script in txt
file and save it as Powershell.ps1
On running this the output should
look like this
·
Accepting inputs and doing something
--Start of Script---
#Test.ps1 This
is will add comment to Test powershell script
param (
[string]$action = "$(Read-Host ' Please
specify the operation which you want to carry - [Add] [Retract] [Deploy]
[Remove]')"
)
#Add
Add-PSSnapin
"Microsoft.SharePoint.Powershell"
function
AcceptInput()
{
if ($action -eq
"Add") {
Write-Host -f
Yellow Do something if condition matches
}
}
AcceptInput
#This will make
sure the output does not disappear
powershell
–noexit
--End of Script--
You can copy paste the script in txt
file and save it as Powershell.ps1
On running this the output should
look like this
Keep watching this space for more
advanced scripts,would publish it soon