Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

How to get GUID of a Sharepoint Site or Site Collection using PowerShell script in Sharepoint 2007/2010/2013/2016

As we all know there are two ways to get a GUID of a Site or Site Collection
1) Using Powershell
2) Using Sharepoint Central Admin

Writing this post as I had a requirement in office where I had to add some users to a Sharepoint group using a console application.
 
This post explores the first method of obtaining the GUID of a site using powershell script –

Below is the Powershell Script
Add-PSSnapin Microsoft.Sharepoint.Powershell
Write-Host "****** Welcome to Powershell script that gives GUID of a Sharepoint Site ******"
Write-Host ""
$UserEntered = Read-Host -Prompt 'Enter the Sharepoint site with port to get GUID (Press Enter after Pasting) '
$site = Get-SPSite $UserEntered
$siteguid = $site.id
echo $siteguid
Write-Host ""
$User = Read-Host -Prompt 'Press Enter Button to Exit'

How to Check a directory exists and create the folder if it does not exists using Powershell

We can use the If condition, Test-Path and New-Item to check and create a directory if it does not exists

Syntax:  if(!(Test-Path -Path <Complete Path>))
        {
          New-Item -ItemType directory -Path <Complete Path>
        }

Example:
if(!(Test-Path -Path C:\Development ))
{
New-Item -ItemType directory -Path C:\Development
}

How to create a folder or sub directory using Windows Powershell

In Windows PowerShell, Directory can be created using the New-Item cmdlet

Syntax:  New-Item -ItemType directory -Path <CompletePath> 

Example:
i) New-Item -ItemType directory -Path C:\Development
This will create directory named Development inside the C drive.

ii) New-Item -ItemType directory -Path C:\Development\FileTransferProject\Output
This will create a directory named Output insidide the FileTransferProject folder, here if the sub directory that is FileTransferProject was not present than FileTransferProject folder will also be created and then the folder Output will be created

Note - If the Directory already exists, then PowerShell will exit without creating the directory.