Saving txt as powershell file

  • Good day Experts,

    How can i save the following script in a powershell file. The script is copied to txt document but i want to change it to .PS 1 file.I tried save as but it is not giving me ps1 option.

    Function out-DataTable

    {

    $dt = new-object Data.datatable

    $First = $true

    foreach ($item in $input){

    $DR = $DT.NewRow()

    $Item.PsObject.get_properties() | foreach {

    if ($first) {

    $Col = new-object Data.DataColumn

    $Col.ColumnName = $_.Name.ToString()

    $DT.Columns.Add($Col) }

    if ($_.value -eq $null) {

    $DR.Item($_.Name) = "[empty]"

    }

    elseif ($_.IsArray) {

    $DR.Item($_.Name) =[string]::Join($_.value ,";")

    }

    else {

    $DR.Item($_.Name) = $_.value

    }

    }

    $DT.Rows.Add($DR)

    $First = $false

    }

    return @(,($dt))

    }

    $hostname = $env:computername

    $AfterDate = Get-Date

    $dataTable = Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" -computer $hostname | Select ProviderName, SystemName, DeviceID , VolumeName, Size, FreeSpace | out-DataTable

    $connectionString = "Data Source=localhost; Integrated Security=True;Initial Catalog=master;"

    $bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString

    $bulkCopy.DestinationTableName = "dbo.DriveStatus"

    $bulkCopy.WriteToServer($dataTable)

  • Have you tried using the Powershell ISE to edit and save?

  • you are probably using notepad to save your file?

    you have to change the file type in the save as dialog.

    Change it to all files and enter the extension manually, ie myPowershell.ps1.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Switch text editors to something like Notepad ++ or use the PS ISE.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply