Showing posts with label Powershell ISE. Show all posts
Showing posts with label Powershell ISE. Show all posts

Tuesday, September 27, 2016

How do you get date and time information using PowerShell?

PowerShell has a cmdlet for most of the usual things including date and time.

The basic cmdlet to get the current date and time is Get-Date.

Start Windows PowerShell by typing powershell in the search box (Windows 10).


PowershellSearch.png

Click Windows PowerShell ISE to open the window shown.


PowershelSE.png

Enter Get-Date in the top pane and click the right pointing arrow to run the script.



PowersheGetDateCmdLet.PNG

You immediately get the response in the bottom pane as shown.

 PowerShellGetDateResponse.png

Now you can the date and time parts of current date using the DisplayHint parameter specifying which part you need. Here is the modified script to get the date part.

The ISE has intellisense and you do get support as shown

PowersheGetDateDisplay.PNG


PowersheGetDateParams.png
Click on DisplayHint and enter a space to get the params that you can access with the DisplayHint


PowershellDisplayHintParams.png

Run the script as shown below in the top pane and hitting the 'green' arrow and you will see the following in the bottom pane.
Get-Date -DisplayHint Date
The response in the bottom pane: 
PS C:\Users\Jayaram> Get-Date -DisplayHint Date
Tuesday, September 27, 2016

How to get the year part of the date?
If you pick year from the drop-down box, you end up with an error

PS C:\Users\Jayaram> Get-Date -Year
Get-Date : Missing an argument for parameter 'Year'. Specify a parameter of type
'System.Int32' and try again.
At line:1 char:10
+ Get-Date -Year
+          ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingExcept
   ion
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.GetDateCo
   mmand

In order to get around this, you change it to:
PS C:\Users\Jayaram> $x=Get-Date
$x.Year
2016

Another way would be to get the date in a string; split it and take the last part as in the following:

$g="Tuesday, September 27, 2016"
$y=$g.Split()
$y[3]
PS C:\Users\Jayaram> $g="Tuesday, September 27, 2016"
$y=$g.Split()
$y[3]
2016

Wednesday, March 2, 2016

How can I backup a SQL Database to a folder on my computer?

You can do it using SQL Server Management Studio(SSMS) or Windows PowerShell. If you are a 'click' expert you would choose SSMS, but if you are a 'dba' type you would rather choose the key board.

The backup-sqldatabase commandlet in Windows Powershell can do it in no time at all. There are only two lines of code.

Let me show the database Northwind in the Object Browser of my SQL Server 2012 Instance.
The name of the instance with the domain is Hodentek8\RegencyPark. On this instance there are several databases. we will backup 'Northwind'


SQLServerRPark.jpg

Launch Windows PowerShell ISE by typing in 'Windows PowerShell ISE' in the searchbox('cortana') in Windows 10.

LaunchISE.jpg

In the Script Pane of Windows PowerShell ISE type in the following lines as shown:


backupcode.jpg
------------
$sqlServer = new-object ("Microsoft.SqlServer.Management.Smo.Server") "Hodentek8\RegencyPark"
Backup-SqlDatabase -ServerInstance "Hodentek8\RegencyPark"
                   -Database "Northwind"
                   -BackupFile "\\HODENTEK8\Users\Public\nwind.bak"

------------
After this code is processed the backup file will be found in the UNC location shown above.

You may want to review this post:
http://hodentekhelp.blogspot.com/2016/08/what-is-easy-way-to-move-database-from.html

Monday, July 13, 2015

How do you customize Windows PowerShell ISE?

You can change many of the settings as well as fonts & Colors in the Windows PowerShell ISE.

Code coloring, intellisense settings; script pane  customization; and other settings can be changed in Windows PowerShell ISE.

Launch Windows PowerShell ISE from the run program as shown:


CusotmizePSISE.png

In the Windows PowerShell ISE displayed click to open Tools | Options to display the Options window as shown. In the tabbed page (2 tabs) General you will see the following. These are (may be) the defaults.


PsIseOptions1.png

In the tabbed page Colors and Fonts you can set the changes you want to make or use defaults for Script Pane as well as Console Panes.


PsIseOptions2.png

Saturday, March 14, 2015

How do you install Windows PowerShell 4.0 on Windows 7?

Windows PowerShell 3.0 is installed on Windows 7 by default but not Windows PowerShell 4.0.
You can check for version and verify if you have Windows PowerShell 4.0 as detailed in the following post:
http://hodentekhelp.blogspot.com/2014/08/if-you-have-multiple-versions-of-power.html

Review this image:

So, how do you install Windows PowerShell 4.0 on Windows 7?

First of all lets take a look at what are the new features that we can use in Windows PowerShell 4.0
Here are the Windows PowerShell 4.0 new features:

 •Support for workflow and remote script debugging
•Improved workflow authoring experience to make it more consistent with script authoring
•Added PipelineVariable as a common parameter
•Better support for downloading updatable help by using Save-Help and Update-Help in offline scenarios
•Updated version from 3.0 to 4.0
•Several bug fixes and performance improvements

Read more here:
http://www.microsoft.com/en-us/download/details.aspx?id=40855

In order to get Windows PowerShell 4.0 you need to install Windows Management Framework 4.0. This can be installed on Windows 7 SP1.
 I am not focussing on Windows Servers for this post.
It is not available for Windows 8.0 but installs by default when you upgrade to Windows 8.1
 
You can download WMF 4.0 from the previous link.
The download  has multiple files as shown here:

WMF40files.png

Installing WMF 40 confers the following functionality:
•Windows PowerShell 4.0
•Windows PowerShell ISE
•Windows PowerShell Web Services (Management OData IIS Extension)
•Windows Remote Management (WinRM)
•Windows Management Instrumentation (WMI)
•The Server Manager WMI provider
•A new feature for 4.0, Windows PowerShell Desired State Configuration (DSC)

You have a much more power full interface now.