Posts

Managing CSV Files using Import/Export-CSV in Powershell

Image
In the PowerShell series, we are looking into working with files in PowerShell. The first types of files we are covering which are used are CSV(comma separated values) file types. We are going to look into two important cmdlets import-csv/export-csv which are widely used while working with CSV.






We start with the CSV file extension, as this is the most basic one. We will make use of the previous example, where we stored the currently running processes to file:

#Defining file for export $exportedFile = 'C:\temp\exportedProcesses.csv' #Exporting as CSV - basic Get-Process | Export-Csv $exportedFile #Opening the file psedit $exportedFile
By default, Export-Csv will write a comma-delimited file using ASCII encoding and will completely overwrite any file using the same name.

Export-Csv may be used to add lines to an existing file using the Append parameter. When the Append parameter is used, the input object must have each of the fields listed in the CSV header or an error will …

How to work with PowerShell Files Read/Write using Set-Content & Get-Content

Image
In the PowerShell script writing series, we are working on some of the helpful areas to write powershell scripts. In this series we would continue covering some of the important topics on working with PowerShell read files, write files,folder,subfolders.
Another area where you should become very confident is working with files (read & write), as you will need to work with them very frequently.

First, we will take a look at the basics of working with files by retrieving and writing files and the content of the files. This can be achieved with the Get-Content and Set-Content/Out-File cmdlets.

First of all, we will take a dedicated look at how you can export content to a file:
#Storing working location $exportedProcessesPath = 'C:\temp\test.txt' #Write processes table to file and show the result in Terminal with the -PassThru flag Get-Process | Set-Content -Path $exportedProcessesPath #Open file to verify psedit $exportedProcessesPath #retrieving processes and exporting…

How to write basic scripts and functions in powershell 6

Image
We are going well with the PowerShell tutorial, we saw how to use credentials and how to work with variables, array, and hash tables.
In the last post, we discussed with the overview of working with script blocks.
In this post, we are going to dig deeper into those with understanding how to design and convert it into functions.
In this post we are going to cover the below topics:

Script vs functionsPass parameters to a scriptConvert to functionsBest practices for designing functions



You can accomplish many tasks in PowerShell by typing a command and pressing Enter in the shell console.        We expect when you are starting up writing commands on the PowerShell console and getting the required output.
      But you hand off a task to someone else and need to make sure that it’s done exactly as planned.
      That’s where scripts come in—and it’s also where functions come in.

SCRIPT OR FUNCTION?

Suppose you have some task, perhaps one requiring a handful of commands in order to complete.
 A scri…

How to work with Variables, arrays, hash tables, and script blocks in Powershell

Image
I have been recently working mostly on PowerShell /Azure, hence decided to covers lots of easier topics from this space. So that you can get familiar with this. In the previous post, we looked into Credentials and how to use them while automating things. In this post we are covering ·Variables ·Strict mode ·Variable drives and cmdlets ·Arrays ·Hash tables ·Script blocks


VARIABLES

Variables are a big part of any programming language or operating system shell, and PowerShell is no exception. In this post, we’ll explain what they are and how to use them, and we’ll cover some advanced variable-like data structures such as arrays, hash tables, and script blocks.

Variables are important for the creation of good scripts.
A variable is like a placeholder and every kind of object and type can be stored in that variable.
You can save data that is often used within a script in it or you can make calculations with it. You can forward variables containing data to functions, to make changes or output with the…

How to work with Credentials Parameter in powershell 6

Image
In the previous post, We worked on to get started with Windows PowerShell. We looked at setting up the PowerShell environment and running a sample Script.
In this post, we will learn and understand an important part while automating things is working with credentials. While many of the cmdlets parameters support credentials.
Most of those cmdlets, whether you work on PowerShell Core or Windows PowerShell, can be executed remotely and with different credentials.
In order to see which cmdlets support a Credential parameter, you can use the ParameterName parameter with Get-Command to discover them.




Examples: Get-Command -ParameterName Credential

First of all, we need to see what a credential actually is by looking at the following code :
$username = ‘venkat’ $password = 'P@ssw0rd' | ConvertTo-SecureString -AsPlainText -Force $newCredential = New-Object -TypeName pscredential $userName, $password $newCredential.GetType().FullName $newCredential | Get-Member
Looking at the code, you…

Powershell 6 -Getting Started with Automating System Administration

Image
Windows PowerShell is a task-based command-line shell and scripting language designed specifically for system administration.
Built on the .NET Framework, Windows PowerShell helps IT professionals and power users control and automate the administration of the Windows operating system and applications that run on Windows.
This post is to help you get up and running with PowerShell, taking you from the basics of installation to writing scripts and web server automation.
This will act as an introduction to the central topics of PowerShell, from finding and understanding PowerShell commands and packaging code for reusability right through to a practical example of automating IIS.
You will explore the PowerShell environment and discover how to use cmdlets, functions, and scripts to automate Windows systems.





Accessing the PowerShell Console and PowerShell ISE
We will see how to access the PowerShell console and PowerShell ISE Console and work with PowerShell 6 module.
We will cover how to :
T…